When navigating the mouse over the screen you only change two coordinates. A mouse cursor lives in a 2D world. In this article I will explain how you can get the position of a mouse click in a 3D world.

First, lets create a new Unity project. Name it as you like. Once the project is created, add a plane. scale the plane up. Next, ad some cubes and spheres.

Your scene should look something like below (does not need to be exactly the same, just add the plane, some cubes and spheres).

Next, Create a new material. Make it red. Also, ad another small sphere to your scene, drag the material to it so that you have a red sphere. Reset the sphere’s position (all 0). Now drag the sphere from the hierarchy to your assets folder. This will create a prefab from the red sphere. Rename the prefab to “RedSphere”, and delete it from your scene. We will use this red sphere to indicate where the mouse click appeared.

We want the game window to show the same as the designer window. For that purpose, make sure the designer/scene window shows all cubes and spheres. Select the camera, go to the menu [Game Objects]>[Align with view] to align the camera.

Now we need to make a script. This script will handle the mouse click. The script has to be attached to a gameobject in order to be activated in the screen. We will attach it to the camera. Create a new script, call it “ClickLocation” and add it to the camera.

Open the script.

To determine the click position of the mouse we will use ray casting. We will send a ray from the position of the camera to the position of the mouse. At the place where the ray collides with any object, that is where the mouse click is positioned.

To make this more visual, we will instantiate the red sphere prefab at the position of the mouse click. For this we’ll need a reference to the prefab in the script.

At the top of the script, make a serializable field of the type GameObject, with the name “redSphere”:

Save the script and return to the editor. The new field should now appear in the inspector:

Now drag the RedSphere Prefab to the Red Sphere field, or click the little circle at the right of this field and select the RedSphere from the list:

Now go back to your script. We will put the logic in the update method. This is what we will do:

1. Check if the mousebutton is released (we want the position of the mouse cursor when you release the left button);
2. If so, create a ray from the camera to the mouse position;
3. Cast this ray ( “fire the laser”). If it hits something, return the position where this hit occurred;
4. Instantiate a red sphere at the hit position.

Use the following code to accomplish this:

Save your script and run the game. When you click and release your button, a red sphere will be placed in 3D space!

In Blender, Shift-select the animated object ans its armature. So both are selected.

Go to File->Export, select FBX.

Next, browse to the folder where you want to export to, and enter an appropriate name.

Make sure to set the following configuration:

1. Check “Selected Objects”

2. At the right bottom, open the “Armature” settings and check “Only Deform Bones”, uncheck “Add Leaf Bones”

3. At the right top shift-select “Armature” and “Mesh”

Click “Export FBX”.

In Unity, Create a new folder under “Assets”. Give it an appropriate name (“Animations” in this example).

Now drag and drop your exported FBX file into this folder.

Done. From here on, you can setup and use the animation in Unity.

This is a short post. It is short because it presents a very simple solution. However, it is a solution to a problem that took me two days to solve.

As I do not want you to lose the same time as I did, I will describe the problem in a few sentences. I will state the keywords below, just to make sure you find this post if you have the same problem … I couldn’t find the solution myself in any forum, I found out by having some luck 🙂 And it is so simple….

The problem:

When you use multiple meshes that are connected to each other, e.g. for modulair walls, or if you use multiple point lights, you might end up with an annoying effect: not all meshes get the same amount of light. The image below demonstrates this. It is a screenshot from a scene in Unity, presenting two identical walls, with two identical lights. There is not any other light source in the scene. Take a look at the difference in lighting:

Different light effect on identical meshes

A more real-world example can be seen in the image below:

Different light effect on identical meshes #2

The walls are simple quads, with the same (seamless) texture applied to it. The scene has multiple lights.

How to solve this? It is very simple! Simply go to the lights that shine or should shine on the wall or that should somehow affect your wall. Next, in the inspector set the Render mode to Important.

That’s it! Check the image below:

Lighting fixed by setting the light sources render mode to important

Problem Solved! … took me two days experimenting with textures, different meshes, shading, smoothness … the solution was build into Unity all the time 🙂

Keywords: Unity 3D Light, Bug, Light Error, same light different effect, light interference, light shows seams, light shines differently on multiple objects.

Do you like the scene with the tables? You can get it here: