# Earth Rotation helped!

Do anyone know how to make a level selection scene where there is a earth rotating and when i press the up or down arrow key, the earth would rotate to a fixed position in a array? So that i can select the level when the earth is rotated to the position. Just like little big planet.

You’d have to store an array of either Vector3’s (as orientation vectors), or an array of Quaternions (for straight up rotations)…

Then, when you select a target, have the planet object Lerp to the target rotation, like this:

``````void Update()
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSharpness * Time.deltaTime);
}
``````

That’ll make it gradually rotate from it’s current orientation, to the target orientation defined in the array.

Depending on how you choose to store your rotations, you will have to extract a Quaternion from the stored data… if it’s a Vector3, storing an orientation vector, it’d be something like
targetRotation = Quaternion.LookRotation(transform.position - storedOrientation, Vector3.up);

If it’s euler angles (as in X, Y and Z angles), it’d be
targetRotation = Quaternion.Euler(storedEulerAngles); if the angles are stored in a vector,

or targetRotation = Quaternion.Euler(storedX, storedY, storedZ); if you’ve stored each angle by itself.

Hope this helps.

Cheers

thank you very much… I Try your method, but when i pressed “up” it would not rotate and every time i pressed “down” it will go to the last Array instead. Could you help me check my error?

``````//--- variables.
var PinPosition: Quaternion[];

//--- private variables.

function Update () {
RotateEarth();
}

function RotateEarth(){
for(var i: int=0; i < PinPosition.length;i++){
if(Input.GetButtonDown("down")){
if(transform.rotation == PinPosition[i]){
transform.rotation = Quaternion.Lerp(transform.rotation, PinPosition[i+1], 50 * Time.deltaTime);
}
}
else if(Input.GetButtonDown("up")){
if(transform.rotation == PinPosition[i-1]){
transform.rotation = Quaternion.Lerp(transform.rotation, PinPosition[i-1], 50 * Time.deltaTime);
}
}
}
}
``````

Oh, ok, I see you’ve gone a little overboard with the loops there

There’s no need to run through each element of the array like that. What that code is doing is, every frame, it checks if the up or down button is pressed for each element in the array.

That’s really not needed, since the state of the input won’t change in the middle of the update, and will most likely cause weird behaviour.

In fact, you don’t need to run through the list of rotations at all, see:

``````var PinPosition[] : Quaternion;

var currentTarget : int = 0;

function Update()
{
if (Input.GetButtonDown("up")) currentTarget   = (currentTarget + 1) % PinPosition.Length;
if (Input.GetButtonDown("down")) currentTarget = (currentTarget - 1) % PinPosition.Length;

transform.rotation = Quaternion.Lerp(transform.rotation, PinPosition[currentTarget], 50 * Time.deltaTime);
}
``````

That oughtta do it

As you can see, we never go through the entire list. We don’t have to, really… all we need is to know the size of the list, and keep track of the index (our position on the list).

So we have an int variable keeping track of it.
The two first lines in the update function are the target selection. Pressing up will add one to the current position, and the % (mod) operator will have the index loop back to 0 if it reaches the end of the list (or else you get an out of bounds error).

Then, the last update line does the rotating for us… It’s safe to let it run every frame, since it has a very definite target and unless the target changes, it will stay oriented as you want it. (that’s also good because it’s safe to mash the up/dpwn buttons, and the planet will be able to change targets even if it’s already moving)

Hope this helps

Cheers

Thank you very much:smile: I am grateful