Created
July 4, 2017 21:33
-
-
Save matt123miller/3a97d818f66aaafcbe09dabbe73ed1bb to your computer and use it in GitHub Desktop.
This file contains 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
/// <summary> | |
/// This rotates the camera to match the orientation of the phone | |
/// </summary> | |
public void TiltPhone() | |
{ | |
// Helps to stop us getting caught in a bad change of states. Resets in ResetRotation(). | |
if (!tilting) | |
{ | |
// Save our current rotation before doing anything else. This is where we'll return later. | |
_resetRotation = transform.localRotation; | |
// This is the opposite of the phones rotation when entering the tilt mode. | |
// We are aiming to negate by this value later. | |
_negatePhoneRotation = Quaternion.Inverse(DeviceRotation.GetRotation()); | |
tilting = true; | |
//debugText.enabled = true; | |
} | |
// None! This is 1 rotation offest by another. No idea how it works. | |
// Why do you offset the right by the left? Who knows. It's magic. | |
_desiredRotation = _negatePhoneRotation * DeviceRotation.GetRotation(); | |
// Set rotation at the end, assumes _desiredRotation has been set in 1 of the above if statements. | |
transform.localRotation = _desiredRotation; | |
// Cache it back into the conveniently shorter variable name. | |
_currentRotation = transform.localRotation; | |
//debugText.text = _desiredRotation.ToString(); | |
} | |
public void ResetRotation() | |
{ | |
// Include anything special that needs to be reset separate to the coroutine. | |
tilting = false; | |
//debugText.enabled = false; | |
StartCoroutine("LerpCameraBack"); | |
} | |
// This works perfectly in all my test so far. | |
private IEnumerator LerpCameraBack() | |
{ | |
// How do I find the return angle and rotate back to normal? So scared. | |
// Quaternions are the devil | |
_currentRotation = transform.localRotation; | |
// We will return to this point. desiredRotaiton doesn't change throughout the whole coroutine. | |
_desiredRotation = _resetRotation; | |
float lerpCompletion = 0; | |
while (lerpCompletion < 0.99) | |
{ | |
lerpCompletion += Time.smoothDeltaTime * lerpMultiplier; | |
var curveValue = curve.Evaluate(lerpCompletion); | |
transform.localRotation = Quaternion.Slerp(_currentRotation, _desiredRotation, curveValue); | |
yield return null; | |
} | |
// The reset rotation has now finished, set any states and variables that need setting. | |
// Sets the rotation back, compensate for any remaining angle changes. | |
transform.localRotation = _desiredRotation; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment