Slerp Rotating 1 Degree and Limiting All Other Rotation
The title may be sort of confusing, but let me explain.
So I'm building a game in which you can flip your local gravity, not the global, but your gravity and yours alone. I got the gravity flipping correct but having you immediately snap to 180 degrees z is kinda disorienting.
So, I've been trying to make a function that slowly rotates the player. There's obviously something wrong with my slerp function as that's where the actual rotation lies. But, I'm only rotating one degree on the z axis and I cant rotate on any other axis, ie looking around. Here is a my code: //Flip Input if (Input.GetButton("Flip")) gravFlipInput = true; else if (Input.GetButtonUp("Flip")) gravFlipInput = false;
//Check If Flip Is Possible and If So, Flip
if (gravFlipInput && !flipping && Grounded())
{
flipping = true;
turning = true;
DIRECTION = DIRECTION ? false : true;
flipGrav = DIRECTION ? -1 : 1;
}
//Check if Flip is Completed
if (flipping && Grounded())
flipping = false;
//Actual Flip Process
if (turning)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z + 180)), flipInterpolationRatio);
if (turning && (transform.rotation.z == 180 || transform.rotation.z == 0) && flipcancel)
{
turning = false;
flipcancel = false;
}
//prevents noting starting rotation
IEnumerator FlipCancel()
{
yield return new WaitForSeconds(1);
flipcancel = true;
}
Answer by xxmariofer · Jan 05, 2021 at 09:03 AM
Well that code dont make much sense so my suggestion is to try to refactor it using coroutines, but here, if the rest of the code is fine, should be an easy fix
if (Input.GetButton("Flip")) gravFlipInput = true;
else if (Input.GetButtonUp("Flip")) gravFlipInput = false;
//Check If Flip Is Possible and If So, Flip
if (gravFlipInput && !flipping && Grounded())
{
flipping = true;
turning = true;
StartCoroutine(Flip())
DIRECTION = DIRECTION ? false : true;
flipGrav = DIRECTION ? -1 : 1;
}
//Check if Flip is Completed
if (flipping && Grounded())
flipping = false;
if (turning && (transform.rotation.z == 180 || transform.rotation.z == 0) && flipcancel)
{
turning = false;
flipcancel = false;
}
IEnumerator Flip()
{
Quaternion target = Quaternion.Euler(new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z + 180));
while (turning)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, target, flipInterpolationRatio);
}
}
//prevents noting starting rotation
IEnumerator FlipCancel()
{
yield return new WaitForSeconds(1);
flipcancel = true;
}
Your answer
Follow this Question
Related Questions
Preventing A Teleporting GameObject From Passing Through Walls 2 Answers
How to change rotation for a FirstPersonController through script. 0 Answers
Flip 3D Character Rotation 180 on Y axis 1 Answer
Rotate transform to match another, but not past 180° 0 Answers
How do I make a script for camera recoil recovery? 0 Answers