Why my code to rotate object doesn't work in normal runtime, but does in debugging?
I'm new to unity, so i thought that my code had a bug that i couldn't find, but i could find the answare on the internet. I was wrong. I've being scraching my head for days now with this problem i have and now its even worse, because when i try to use the debugging tool from Visualstudio it works like intended. So i wan't the player to be able to change the gravity direction by 90° on command so that it can walk on any side of a room for example. I got that working with no problems so i tried making the rotation of the character go smoothly and that what got me the problem. I tried using Quaternion.lerp but that only rotates the object on its own axes and i want it to rotate on the world axes. So i staied with transform.Rotate but the thing is that it always rotates more than 90°. I used the debugging tool and that never hapens. Here is my code:
private void Start()
{
rotationEnable = true;
}
private void Update()
{
}
private void FixedUpdate()
{
if (m_gravityIsRotating) StartCoroutine(ApplyGravityRotate(rotationDuration));
m_gravityIsRotating = false;
}
private IEnumerator ApplyGravityRotate(float durationInS)
{
float timeElapsed = 0f;
do
{
float deltaTime = Time.deltaTime;
timeElapsed += deltaTime;
if (timeElapsed > 1)
{
deltaTime = 1 - (timeElapsed - deltaTime);
}
if (xRotation)
{
transform.Rotate(90 * deltaTime * negativMultiplier, 0, 0, Space.World);
}
if (yRotation)
{
transform.Rotate(0, 90 * deltaTime * negativMultiplier, 0, Space.World);
}
if (zRotation)
{
transform.Rotate(0, 0, 90 * deltaTime * negativMultiplier, Space.World);
}
yield return new WaitUntil(() => !m_gravityIsRotating);
} while (timeElapsed < durationInS);
xRotation = false;
yRotation = false;
zRotation = false;
rotationEnable = true;
}
public void OnGravityLeft(InputAction.CallbackContext context)
{
float lookDirectionX = Math.Abs(transform.forward.x);
float lookDirectionY = Math.Abs(transform.forward.y);
float lookDirectionZ = Math.Abs(transform.forward.z);
m_gravityIsRotating = true;
if (context.performed && rotationEnable)
{
rotationEnable = false;
if (lookDirectionX > lookDirectionY && lookDirectionX > lookDirectionZ)
{
xRotation = true;
if (transform.forward.x > 0) negativMultiplier = -1;
else negativMultiplier = 1;
}
else if (lookDirectionY > lookDirectionZ)
{
yRotation = true;
if (transform.forward.y > 0) negativMultiplier = -1;
else negativMultiplier = 1;
}
else
{
zRotation = true;
if (transform.forward.z > 0) negativMultiplier = -1;
else negativMultiplier = 1;
}
}
}
Your answer
Follow this Question
Related Questions
Why won't my model rotate up on X axis? 1 Answer
my camera is rotating on Z axis when i just set it on X and Y 0 Answers
How can I make the camera not overreach a limit of rotation one axis. 0 Answers
How can I add inertia to a rotation when I release the mouse? 1 Answer
How to apply script to a imported OBJ file in unity? 0 Answers