Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unable to rotate 2D object along z axis via keyboard inputs

Discussion in 'Scripting' started by RayDawg, Feb 2, 2015.

  1. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I'm making a top-down game and I can't seem to get the sprite to rotate along the z-axis whenever I press the input keys. Here's what I have so far, could someone please help? As it stands, I can manually rotate the object along the z-axis and when I press the input keys, the object will always rotate facing the top of the screen now matter what angle.

    Code (CSharp):
    1. void KeyPresses()
    2.     {
    3.         float MoveHorizontal = Input.GetAxisRaw("Horizontal");
    4.         float MoveVertical = Input.GetAxisRaw("Vertical");
    5.  
    6.         Vector3 PlayerRotation = new Vector3(MoveHorizontal, MoveVertical, 0.0f);
    7.        
    8.         rigidbody.velocity = PlayerRotation * movementSpeed;
    9.  
    10.         if (PlayerRotation != Vector3.zero)
    11.         {
    12.             // Set the new rotation
    13.             newRotation = Quaternion.LookRotation(PlayerRotation);
    14.  
    15.             // Use the new rotation as a target to rotate towards
    16.             transform.eulerAngles = Vector3.forward * Mathf.MoveTowardsAngle(transform.eulerAngles.z, newRotation.z, 360 * Time.deltaTime);
    17.         }      
    18.     }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I don't quite understand what you're trying to do here. If you just want to rotate around Z, that only requires one axis, and the code would look something like this (untested):

    Code (CSharp):
    1. void KeyPresses() {
    2.         float MoveHorizontal = Input.GetAxisRaw("Horizontal");
    3.         transform.Rotate(0, 0, MoveHorizontal);
    4. }
     
  3. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I'm trying to get a 2D sprite to move up, down, left, and right. I also want it to face the correct direction by rotating towards that particular button direction and then start moving instead of instantly rotating.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Ah, well that's different then!

    Getting it to move is easy enough. Making it rotate to face the particular direction and then move is considerably harder.

    I probably can't just bang out that much code here in the forum editor that would actually work (though I confess I'm tempted to try!). But here's a sketch of what you need to do:

    1. Add a "targetAngle" property that keeps track of what angle your sprite wants to face. Make it a public property, and during initial testing, you can just manually set this to make sure the rest of it works.
    2. In your Update method, use Mathf.MoveTowardsAngle to move your current rotation towards this targetAngle. (Test this via the inspector, as described above.)
    3. Now, throw some if-statements at the problem of setting your targetAngle from the horizontal and vertical input axes. Test again, but now you should be able to make the sprite smoothly face the desired direction just by pressing the movement keys.
    4. Now add a "moveSpeed" property, and code to set this according to the horizontal/vertical inputs. Basically this should be the greatest absolute value of either input, so it's 1 when you're holding a movement button, and 0 when you're not.
    5. Finally, when your current angle is equal to your targetAngle, you should move forward in proportion to moveSpeed.

    That should do it!