Created
September 27, 2011 15:15
-
-
Save onedayitwillmake/1245337 to your computer and use it in GitHub Desktop.
Rotating a quaternion to face a relative target overtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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