简体   繁体   中英

How do I rotate an object on the Y axis?

I'm trying to make a game with a 'rotating' Earth, but I don't know how to rotate... This is what I've got so far, help would be much appreciated:

using UnityEngine;

public class Earth_Rotation : MonoBehaviour
{
     // Update is called once per frame
     void Update()
     {
     Transform.Rotate (0, 10, 0);
     }
}

You need to call the Rotate() method on your objects transform component. Uppercase Transform refers to the Transform class itself where lowercase transform refers to this objects instance of a transform component. If you want to manipulate the object your script is attached to you need lowercase transform . I suggest checking out this link to learn about classes and objects: Class and Object - GeeksforGeeks

Further, if you want your object to turn over time, you need to reference the time somewhere. This can be achieved via Time.deltaTime which returns the time passed since the last frame in Unity. Try something like this:

void Update()
{
    //Vector3.up is a vector that looks like this: (0,1,0)
    transform.Rotate(Vector3.up * Time.deltaTime);
}

You can also add a modifier like public float turnSpeed and multiply by that to increase or decrease your objects turn speed:

public float turnSpeed;

void Update()
{
    transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
}

If you set turnSpeed = 10 you have your original value.

Always take a look at the documentation for the functions you are trying to use. It helps a lot to understand how and where to use them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM