2D Movement Rotation problem

First off, I am very new to Unity, but have hobbyist experience with making games.

My problem is a weird one and most likely because I’m not up to speed on how Unity works. I have the following code assigned to my player object:

void Update () {
        // Ammount to move
        float amtToMoveX = Input.GetAxisRaw("Horizontal") * fPlayerSpeed * Time.deltaTime;
        float amtToMoveY = Input.GetAxisRaw("Vertical") * fPlayerSpeed * Time.deltaTime;
        
        // Move the player
        transform.Translate(Vector3.right * amtToMoveX);
        transform.Translate(Vector3.up * amtToMoveY);

        // Rotate player
        float angle = Mathf.Atan2(amtToMoveX, amtToMoveY) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.back);

}

My player object is a triangle, and it rotates correctly to the direction of the movement vector. The rotation always works fine.

The player movement works correctly on its own, when I remove the //Rotate player section. They just don’t work well together.

You can see this behaviour at http://www.bigerstaff.com/WebPlayer/WebPlayer.html

All I am trying to do here, is move the player object via Input.GetAxisRaw and point the triangle in that direction. Am I misunderstanding transform.rotation :face_with_spiral_eyes: is that also trying to move the player object, even though the player object stays stationary when I remove the //Move the player section?

I hope that makes some sense :stuck_out_tongue:

Thanks for reading!

David

Fixed this myself and thought it polite to put my solution.

void Update () {
        // Ammount to move
        vDir.x = Input.GetAxisRaw("Horizontal");
        vDir.y = Input.GetAxisRaw("Vertical");

        // Rotate player
        float angle = Mathf.Atan2(vDir.x, vDir.y) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.back);

        // Move the player
        transform.Translate(Vector3.up * vDir.magnitude * fSpeed * Time.deltaTime);
    }

My eureka moment came when I realised that when I was rotating the player object, it was also rotating the relative Vector3.right and Vector3.up that I was using in transform.Translate.

Realising that, all I need to do is rotate the player object in the direction of the input, and apply movement in only the Vector3.up direction!

Are there any other ways I should be doing this? Am I doing anything particularly backwards?

Thanks for looking.

2+ years later i find ONLY this solution after a long search!

You are a genius sir!

Just in case I could help someone like me, if you’re making a 2d game where something is following a target while facing him ( i.e. missiles follow player ) add 90 degrees to the result:

var angle:float = Mathf.Atan2( this.position.y - target.position.y, this.position.x - target.position.x) ;
var rotation:float = angle*Mathf.Rad2Deg + 90;
transform.rotation = Quaternion.AngleAxis( rotation, Vector3.forward );

change *Vector3.forward to the corresponding axis you are using. For example in a top-down shooter u might want to use Vector3.up etc…