At the moment I have the following code:
if (Input.GetKeyDown(KeyCode.A))
{
transform.RotateAround (Vector3.zero, transform.up, 5 * Time.deltaTime);
}
This rotates the object as I want to along the surface of my sphere however I’d like to make it do this 5 degree rotation over y seconds on button press. The most obvious way that I can think of is to have a timer variable however there must be a less convoluted way to rotate the object and have it stop exactly at 5 degrees.
I’d appreciate any help - please let me know if more information is required.
I’m not completely sure of your requirements. Making some assumptions, here is how I might code it if I did not want to use a timer:
#pragma strict
private var sphere : Transform;
private var trCenter : Transform;
private var qTo : Quaternion;
var speed = 5.0;
function Start(){
sphere = GameObject.Find("Sphere").transform;
trCenter = new GameObject().transform;
trCenter.rotation = sphere.rotation;
trCenter.parent = sphere;
trCenter.position = sphere.position;
transform.parent = trCenter;
qTo = transform.localRotation;
}
function Update() {
if (Input.GetKeyDown(KeyCode.H)) {
var axis = Vector3.Cross(transform.right, transform.position - sphere.transform.position);
var q = Quaternion.AngleAxis(5.0, axis);
qTo = q * qTo;
}
trCenter.localRotation = Quaternion.RotateTowards(trCenter.localRotation, qTo, speed * Time.deltaTime);
}
What it does is create a game object and place it at the origin. The physical game object that you want to rotate around the sphere is then made a child of the empty game object. Quaternion.RotateTowards() rotates at a specified speed. In this case I set the seed to 5.0 which means a 5 degree rotation will take 1 second. If you wanted it to take 1/2 second, you could set speed to 10.0. Or if you really wanted to specify time, you could create a time variable and set speed to ‘5.0 / time’ for a five degree rotation. Rotation is applied to the empty game object at the origin, and the physical game object as a child goes along for the ride. To me this is not simpler than using a timer, and it would take some additional code to prevent the keypress during rotation.