Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to get transform.rotation.z to chase the mouse pointer at a rotation speed. But I get a very weird results and can't figure out what I am doing wrong.

var mp = Input.mousePosition;
var camZ = Camera.main.transform.position.z;
// Grab the current mouse position on the screen.
// Subtract the cams Z from the mouse Z to get ground plane. 
var mousePos = Camera.main.ScreenToWorldPoint (
                           new Vector3 (mp.x, mp.y, mp.z - camZ));
//Rotates toward the mouse                      
var targetEuler = new Vector3 (0, 0, 
      Mathf.Atan2 ((mousePos.y - transform.position.y), 
                   (mousePos.x - transform.position.x)) * Mathf.Rad2Deg - 90);
transform.eulerAngles = new Vector3 (0, 0,
      Mathf.Lerp (transform.eulerAngles.z, targetEuler.z,
                  Time.deltaTime * rotationSpeed));
share|improve this question

OKi was way off. This is how i got it to work.

                        rotationTarget = Camera.main.ScreenToWorldPoint (Input.mousePosition);                      

                    var _lookRotation = Quaternion.LookRotation (transform.position - rotationTarget, Vector3.forward);

                    _lookRotation.x = 0.0f;

                    _lookRotation.y = 0.0f;

                    transform.rotation = Quaternion.Slerp (transform.rotation, _lookRotation, Time.deltaTime * rotationSpeed);  
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.