Trouble Making 2D Object Follow Mouse A Certain Way

I’m having some serious problems having my player object rotate toward the mouse direction (on mouse click) and travel along its transform.right axis. Similar to how a car is always traveling forward, only its forward is constantly pointing different ways - you get the picture. Before you click away, yes I’ve done extensive searches for a solution and have been unsuccessful. I can’t identify the problem. I’m completely nonplussed! I’m here, I have my code, and I even drew you some pictures and everything.

Here’s a picture to illustrate my point. Please excuse my bad handwriting.

What I would like to happen: The player character is always moving along its own right axis. When the player clicks (or holds) the mouse, the player character rotates toward the click (while still moving). The goal is to have a sort of arc happen while it turns. Like this:

Here is the commented code that should make such a thing happen.

public float moveSpeed;             // movement speed

    public float turnSpeed;         // rotation speed


    private Vector3 moveDir;        // desired rotation



    void Start () 





    void Update () 



        // store current position each frame

        Vector3 curPos = transform.position;    


        // while mouse is clicked

        if (Input.GetButton ("Fire1"))


            // store mouse position

            Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);


            // get a direction vector, assign to moveDir

            moveDir = mousePos - curPos;

            // make sure there is no movement along z

            moveDir.z = 0;

            // normalize direction vector




        // Get angle of direction vector. Convert to degrees

        float targetAngle = Mathf.Atan2 (moveDir.y, moveDir.x) * Mathf.Rad2Deg;

        // rotate player from current rot to targetAngle (as Euler)

        transform.rotation =

            Quaternion.Slerp (transform.rotation,

                              Quaternion.Euler (0,0,targetAngle),

                              turnSpeed * Time.deltaTime);



        // move player along its positive x axis by speed.

        transform.Translate (transform.right * moveSpeed * Time.deltaTime);


It’s not working.

Here’s what’s really happening: the character rotates correctly, its transform points toward the mouse but it’s no longer traveling along its x axis. It’s going somewhere else. It seems to be off by 90 degrees but it’s inconsistent.

I suspect the mistake is happening during the targetAngle calculation. If I click near to the right, where the angle change is minimal, it works. If I click behind the player, where the angle change is drastic, he goes careening off into outer space but he still faces the mouse click

Unity masters, please help me.
Does it have something to do with moving a rigidbody2D through translate instead of using the physics engine?
Is there some glaring error in my logic or code that would be throwing off the angles?
Why does my object seem to forget to travel along its transform.right?

Here are a few more screenshots, these in engine to try to clarify. I’ve added a green ray to always point in transform.right to track the object’s rotation.

Here the click is slightly above the right. everything is in order

Here the click was above the player, resulting in absolute angle anarchy

I realize this post was long. Thank you in advance.

I see two problems:

First, Quaternion.Slerp doesn’t do quite what you think it does. You want RotateTowards.

Slerp takes two rotations, a and b, and a float t, then returns another rotation that is t percent between them. Because of this, your rate of rotation is going to vary wildly based on how far you’re attempting to rotate. Since you’re multiplying by deltaTime, you’re also going to see some unusual behavior to do with frame rate.

RotateTowards takes two rotations, a and b, and a float maxDegrees, and returns a rotation that moves from a to b by up to maxDegrees. That seems much closer to what you’re actually trying to accomplish, here.

Second, Translate’s default behavior is to transform the direction into local space. By passing in transform.right, you’re applying the object’s rotation twice. Either specify Space.World so that the translation expects world coordinates, or pass Vector3.right so that the transform is only applied once.