6

I would like to rotate a sprite on the scene by pressing the left or right arrows keys (think of the spaceship in Asteroids).

I have placed the sprite in question on the scene and created a script, but am not really certain of where to go from there.

My current script looks like this:

 using UnityEngine;
 using System.Collections;

 public class RotateLeftRight : MonoBehaviour {

    public float speed = 1.0f;
    public string axisName = "Horizontal";

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {

        if(Input.GetKeyDown(KeyCode.LeftArrow)) {
            // left
            transform.Rotate(-1.0f, 0.0f, 0.0f);  // does nothing, just a bad guess
        }

        if(Input.GetKeyDown(KeyCode.RightArrow)) {
            // right
            transform.Rotate(1.0f, 0.0f, 0.0f);  // does nothing, just a bad guess
        }

     }
 }

I just coded the above without any knowledge of what would happen (and, hardly surprising, nothing appears to happen at all).

Any advice on how to rotate the sprite and control the speed of the rotation would be greatly appreciated.

Sailing Judo
  • 11,083
  • 20
  • 66
  • 97
  • read through this http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html and make sure that the script is attached to the same object as your sprite – JRowan Nov 18 '13 at 23:22
  • I'm 100% sure the script is attached to the spite. I tried a couple different transform methods mentioned in the link you provided but I'm still having no luck. – Sailing Judo Nov 19 '13 at 00:43
  • 1
    try just Input.GetKey(KeyCode.LeftArrow) instead of getkeydown, thats what i have and it works – JRowan Nov 19 '13 at 00:53
  • it says in this link that getkeydown only fires on the frame that the key is pressed down, getkey fires every frame http://docs.unity3d.com/Documentation/ScriptReference/Input.html – JRowan Nov 19 '13 at 00:58
  • Yes, GetKey was a much better method to call. Thanks for the input here as well. – Sailing Judo Nov 19 '13 at 03:52

2 Answers2

5

I'm not able to try it with Unity right now, but my guess is that it is either rotating just 1º, so you are not able to notice it, or rotating 360º, and so it really stays the same.

Try to break down your problem:

  1. Instead of transform.Rotate try transform.Translate(20f, 20f, 20f) just to make sure it is recognizing the input;
  2. Use a different value instead of 1.0f, such as 0.1f and 30.0f (I think 30.0f would be 30º, but I'm not sure);
  3. Try changing the rotation on the other axes y and z instead of x;
  4. Use the alternative definition Rotate(Vector3 axis, float angle).

Hope it helps!

Jorge Galvão
  • 1,729
  • 1
  • 15
  • 28
  • This worked. Apparently 1 is not a good number to test with. – Sailing Judo Nov 19 '13 at 03:52
  • Actually, using 1 is fine. The only thing matters is to know about the X, Y, and Z axis, @SailingJudo. You should broaden more knowledge on using the parameters of the Rotate function. (See my answer below.) – David Dimalanta Feb 03 '14 at 09:51
3

@Sailing Judo, here's the best answer if you want to rotate it like a wheel. Try observe again to your code and instead of putting/changing the X-axis as parameter, put your value at the Z-axis instead. Changing x or y-axis in a circular rotation ended up like flipping coins. Observe and try again.

   if(Input.GetKey(KeyCode.LeftArrow)) {
        // Clockwise
        transform.Rotate(0, 0, -3.0f); // --> Instead of "transform.Rotate(-1.0f, 0.0f, 0.0f);"
    }

    if(Input.GetKey(KeyCode.RightArrow)) {
        // Counter-clockwise
        transform.Rotate(0, 0, 3.0f); // --> Instead of transform.Rotate(1.0f, 0.0f, 0.0f);
    }
David Dimalanta
  • 548
  • 6
  • 19