Skip to content

Instantly share code, notes, and snippets.

@matt123miller
Created July 4, 2017 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt123miller/3a97d818f66aaafcbe09dabbe73ed1bb to your computer and use it in GitHub Desktop.
Save matt123miller/3a97d818f66aaafcbe09dabbe73ed1bb to your computer and use it in GitHub Desktop.
/// <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