Kevin Cranfield
Kevin Cranfield

Reputation: 51

How to rotate a GameObject more than once

trying to use suggested option have an error vector3 right is not in its definition line transform.Rotate(Vector3.Right

thank you in advance

using UnityEngine;
using System.Collections;

public class ForwardBack : MonoBehaviour {
    // speed variable
    public float speed;


    public KeyCode pressLeft;
    public KeyCode pressRight;
    public float rotationSpeed;

    // Use this for initialization
    void Start () {
        speed = 1f;
    }

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

        transform.Translate(0f, speed*Input.GetAxis("Vertical") *     Time.deltaTime,0f);

         if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(Vector3.Right * rotationSpeed *    Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
                transform.Rotate(Vector3.Left * rotationSpeed * Time.deltaTime);
        }
    }
}

Upvotes: 1

Views: 155

Answers (2)

Danny Herbert
Danny Herbert

Reputation: 2031

There are quite a few inefficiencies in your code. You should not call GetComponent<>() in the Update loop. Instead store a referece to it using the Start function and use that in the Update loop:

public class ForwardBack : MonoBehaviour 
{

    private Transform thisTransform = null;

    void Start () 
    {
        // Get refrence
        thisTransform = GetComponent<Transform>();
    }

    void Update () 
    {
        //Use refrence.
        thisTransform.Rotate(Vector3.right * Time.deltaTime);
    }
}

Note: You might realise that every child of MonoBehaviour inherits the transform property which will look up a reference to the Transform component. Using this is inefficient. You should get your own reference like I have shown here.

Edit:As noted by @JoeBlow the transform property may be fine to use. There is nothing mentioned about how it returns the Transform component in the Unity docs. I have read that it is just a wrapper for GetComponent though from a few different sources. Use at your own discretion.

Secondly, don't use eulerAngles to rotate. It even says so in the docs.

Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees. Use Transform.Rotate instead.

It looks like you're not even incrementing the angle, just setting it to new Vector3(0, 0, -90) anyways which means even if you did do this it will just be set to that value rather than slowly increasing.

The docs for eulerAngles also lead you to what you should be using which is Transform.Rotate.To get this to work you will have to change the input method to Input.GetKeyrather than Input.GetKeyDown but this has already been mentioned in one of the other answers. Then you can rotate around your desired axis. Remember to use Time.deltaTime to scale the value so as it rotates at the same speed no matter what framerate you have. Your code for rotation might look something like this:

public class ForwardBack : MonoBehaviour 
{
    public float roatationSpeed;
    private Transform thisTransform = null;

    void Start () 
    {
        thisTransform = GetComponent<Transform>();
    }

    void Update () 
    {
        if(Input.GetKey(KeyCode.RightArrow)
        {
             thisTransform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.LeftArrow)
        {
             thisTransform.Rotate(Vector3.left * rotationSpeed * Time.deltaTime);
        }
    }
}

Upvotes: 1

Tom
Tom

Reputation: 2472

Try Input.GetKey instead of Input.GetKeyDown

Input.GetKeyDown detects if a button is pressed, whereas Input.GetKey checks for a continuous press. I imagine that is why your cylinder is only turning once.

Upvotes: 2

Related Questions