Reputation: 326
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);
}
}
Upvotes: 1
Views: 2912
Reputation: 479
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.
Upvotes: 4