- Home /
Object Look At Mouse
I've seen a lot of similar questions, but none seem to give me the wanted effect. I have an object in my scene that I want to point at the position of my mouse. Except, it's 2d gameplay so I just want it to point at the x and the y coordinates of my mouse. I've tried different approaches, but it didn't really work out the way I'd liked it too.
Anyone any suggestions?
Answer by Berenger · Mar 08, 2012 at 12:00 AM
If it is a top-down game, you can use Camera.main.ScreenToWorld. This gives you a point to use with LookAt, or by affecting the direction object -> point to the object's forward vector. Just ignore the axe you don't use.
And how would I use Camera.ScreenToWorldPoint to get the position of the mouse?
Camera.ScreenToWorldPoint(Input.mousePosition) gives you the 3D position of the mouse on the near plane. This is relevant only if the camera is orthograpic. If it's perspective, you will need to cast a ray with Physics.Raycast or Plane.Raycast and Camera.ScreenPointToRay.
This looks like it's going the right way, my camera is orthographic and I'm trying to use this piece of code:
var targetmouse : Camera.ScreenToWorldPoint(Input.mousePosition);
ship.transform.LookAt(targetmouse);
Yet somehow I get this error "UCE0001: ';' expected. Insert a semicolon at the end."
Almost there. What comes after the : in a var declaration is the type, not the value. The type here is Vector3. Use = to affect the value.
Note that the position you'll get that way has approximately the same y than your camera (if it's looking down) and the ship doesn't, so it's going to look up and I assume you don't want that. You need to add targetmouse.y = ship.transform.position.y to be perfectly 2d. That's what I meant by "Just ignore the axe you don't use"
So this is the code I'm currently using: var targetmouse : Vector3 = Camera.ScreenToWorldPoint(Input.mousePosition);
targetmouse.x = ship.transform.position.x;
ship.transform.LookAt(targetmouse);
But now it's giving me this error: BCE0020: An instance of type 'UnityEngine.Camera' is required to access non static member 'ScreenToWorldPoint'.
Answer by Taylor-Libonati · Aug 05, 2012 at 08:55 PM
I had a similer problem with my top down game. In my case x is left and right, y is up and down, so I wanted the rotation to be happening around the z axis. If you have a different orientation you will have to edit some of the code.
Here was my solution:
//Aim player at mouse
//which direction is up
Vector3 upAxis = new Vector3(0,0,-1);
Vector3 mouseScreenPosition = Input.mousePosition;
//set mouses z to your targets
mouseScreenPosition.z = transform.position.z;
Vector3 mouseWorldSpace = Camera.mainCamera.ScreenToWorldPoint(mouseScreenPosition);
transform.LookAt(mouseWorldSpace, upAxis);
//zero out all rotations except the axis I want
transform.eulerAngles = new Vector3(0,0,-transform.eulerAngles.z);
Thank you so much! I tried everything else, but this worked perfect
This is the first script that I found that works, but it only rotates about 20 degrees in each direction.
Your answer
Follow this Question
Related Questions
Lock rotation axis? 4 Answers
Gun should point at mouse position 3 Answers
Having a first person player look at the mouse on the y axis 1 Answer
Third person animation 0 Answers
transform.LookAt gives a twist 2 Answers