- Home /
Flashlight looking at mouse in 2D
Simple code, im trying to make a cube follow the position of the mouse, i tried different things but something's not right; the position of the cube is far from the actual position of the mouse pointer, why is that?
//GameObject is the cube
void Update ()
{
gameObject.transform.position = Input.mousePosition;
}
or in javascript:
var mousePos : Vector3;
function Update () { mousePos = Input.mousePosition; gameObject.transform.position = mousePos; }
or just using the lookAt function to make it simpler
transform.LookAt(Input.mousePosition);
im doing this so that i can target a flashlight torwards the cube, this is a 2D game so the MouseLook script wont work; i keep seeing there's something to do with the screen.height but im not rly sure what to do. thx u
Answer by Alec-Slayden · Feb 26, 2011 at 05:14 PM
The mouse operates in screen space, a 2-dimensional(basically) plane in front of the camera, it uses different coordinates and isn't really a 3D thing you can look at.
However, you can have your code turn it into world coordinates by properly using ScreenToWorldPoint
This will make a Vector3 that you can use as position information (specifying your own z coordinate).
for example,
transform.position = myCamera.ScreenToWorldPoint( Vector3(Input.mousePosition.x, Input.mousePosition.y,10));
will set your object's position 10 units directly in front of the camera, at the mouse position.
EDIT:
I changed the code to imply the camera as a variable. You can declare this with your other variables
var myCamera : Camera;
and in the inspector, drag the camera game object into that variable slot.
Well, 10 is a bit far... since 1 unit == 1 meter. The example on ScreenToWorldPoint suggest to use camera.nearClipPlane. But keep in $$anonymous$$d the camera clips at the near plane so you will need to move it a bit further which depends on your object size. If you just want to move a not rendered GameObject eg. with a spotlight then use camera.nearClipPlane.
i rllllyyyy dont get it...
for one yea the mouse operation in a 2D manner but thats what i want anyway, cant u just leave the z coordinate to a static number or something?
and y does the above code i wrote make the object like +300 in the x axes and +500 in the y axes (random number) i mean why isnt it on top of the actual mouse?
and err, wat r world coordinates anyway? wats the difference to just giving it normal Vector3 coordinates?
also i get an error i need to attach a camera to the object that's following the mouse?
sry new to all this.
Yeah thats a good point, Bunny; I was just putting in an arbitrary distance but 10 probably would be far in most scenes :D
@Leon the problem is that the mouse isn't operating in the world space. The mouse's X position is a special number unique to the screen, it's different from the X position of objects in the game.
In order to make it be on top of the actual mouse, you need to have the game interpret the screen points into world points. Thats what the code I provided above does. I'll add in an edit about the camera.
mmm took me a $$anonymous$$ute to get it, it does kinda work but i dont think the way im doing it is right, im not sure why also the light keeps shaking quite a lot, and i still need to write something for the rotation of the flashlight + make it stay on the character, isnt there a simpler way to make a flashlight system for 2D games? thx for the help anyway.
what you should probably do for a flashlight is make the actual object a child of the character you're moving around, and add a spotlight. If you give this spotlight a flashlight cookie (which I believe there are a couple included with Unity's packages). I'm not sure the sort of effect you're going for, but a spotlight is probably going to be handy
Answer by farzamvat · Aug 13, 2015 at 02:23 AM
the position of mouse is in pixel form and it's discrete, you should convert mouse position to unity's world with Camera.main.ScreenToWorld(Input.mousePosition) function ;)