Skip to content

Instantly share code, notes, and snippets.

@onedayitwillmake
Created September 27, 2011 15:15
Show Gist options
  • Select an option

  • Save onedayitwillmake/1245337 to your computer and use it in GitHub Desktop.

Select an option

Save onedayitwillmake/1245337 to your computer and use it in GitHub Desktop.
Rotating a quaternion to face a relative target overtime
//http://blog.infrared5.com/
protected Vector3 relativePos;
protected Quaternion rotationVector;
protected void UpdateRotationVector()
{
relativePos = currentTarget.transform.position - transform.position;
rotationVector = Quaternion.LookRotation(relativePos);
}
Now, in the updateRotation method, we rotate the ship elegantly toward the new way point, and all you have to do is adjust “actualSensitivity” to achieve whatever aggressiveness you’re after:
protected Quaternion rotQ;
protected float yRot = 0;
protected float lastYRot;
protected void UpdateRotation()
{
rotQ = Quaternion.Lerp(transform.rotation, rotationVector, Time.deltaTime*actualSensitivity);
transform.rotation = rotQ;
// this next bit is to get them to bank into their turns
yRot = lastYRot - transform.localEulerAngles.y;
yRot = Mathf.Clamp(yRot, 0, 3);
transform.Rotate(0,0, yRot, Space.Self);
lastYRot = transform.localEulerAngles.y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment