Rotating a spaceship with delay

This migh be a really stupid question, but what i want to do is rotate my spaceship. This is my code so far:

public class Rotation : MonoBehaviour
{
private Rigidbody RigidBody;

void Start()
{
    Rotate();
    RigidBody = GetComponent<Rigidbody>();
}

void Rotate()
{
    transform.Rotate(new Vector3(0.0f, 20f, 0.0f) * Time.deltaTime);
    Invoke("RotateStop", 2);
}

void RotateStop ()
{
    transform.Rotate(new Vector3(0.0f, 0.0f, 0.0f) * Time.deltaTime);
}

}

All i want to do is rotate the object for a curtain amount of time and then stop. My current code adds the delay, but the object just nudges a bit in the direction i want, and then stops.

The reason your code does not work is that Rotate() is only call once by Start() - this will rotate the object a tiny amount. RotateStop() doesn’t actually do anything there, it basically says “rotate my object by 0 degrees”.

I would do this using a coroutine as follows:

using UnityEngine;
using System.Collections;

public class Rotation : MonoBehaviour {
	public Vector3 rotation = new Vector3(100,0,0);
	public float rotationDuration = 2f;

	void Start() {
		StartCoroutine(UpdateRotation());
	}

	private IEnumerator UpdateRotation() {
		float endTime = Time.time + rotationDuration;
		while(endTime > Time.time) {
			this.transform.Rotate(rotation * Time.deltaTime);
			yield return new WaitForFixedUpdate();
		}
	}
}

Hope this helps! :slight_smile: