- Home /
My Code that Works on an object's rotation doesn't work.
I'm coding a system, where if my 3d object moves forward, it's rotation goes down to a certain point, when I try my code, it will keep rotating for ever when I use it (pressing W).... Code:
if (Input.GetKey(KeyCode.W))
{
transform.Translate(0f, 0f, speed * Time.deltaTime); // Forward movement of the player's character.
if (transform.rotation.x <= 20) //If the rotation of the object is below or = 20, make the rotation higher
{
transform.Rotate(rotationSpeedWhileMoving * Time.deltaTime, 0f, 0f);
}
}
else if (transform.rotation.x > 0) //Returns THe object's rotation to 0
{
transform.Rotate(-1 * rotationBackwardsSpeed * Time.deltaTime, 0f, 0f);
}
Answer by Hellium · Aug 07, 2018 at 09:17 PM
transform.rotation
is a QUATERNION, you should not try to read and write values of it, unless you really know what you are doing, and it clearly seems that it's not the case.
You should check against transform.localEulerAngles
instead, which are the values displayed in the inspector:
if (transform.localEulerAngles.y <= 20)
// ...
if (transform.localEulerAngles.x > 0)
No, change transform.rotation.x
to either transform.rotation.eulerAngles.x
or transform.localEulerAngles.x
.
transform.eulerAngles
is the global rotation of the object.
transform.localEulerAngles
is the local rotation of the object, the values shown in the inspector.
What is the difference between local and global rotation?
Answer by tormentoarmagedoom · Aug 08, 2018 at 07:35 AM
Good day.
As @Hellium says, in Unity, standard variable for rotation are QUATERNION (there are not the "typical" 3 rotation of each axis). In the inspector, you see the EULER ROTATION (not quaternion), so if you want to manipulate rotation, you should do it via Euler angles (they are indeed the 3 axis rotation from 0 to 360).
You should check against
transform.localEulerAngles
instead:
Go try it, and if don't suceed, come again!
Bye!
This is where I'm stuck now
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) { transform.Translate(0f, 0f, speed * Time.deltaTime); // Forward movement of the player's character.
if (transform.localEulerAngles.x <= 20) // Transforms the rotation of the object a bit down.
transform.Rotate(rotationSpeedWhile$$anonymous$$oving * Time.deltaTime, 0, 0);
}
else if (transform.localEulerAngles.x > 0 && transform.localEulerAngles.x < 40) //returns the rotation back to normal.
transform.Rotate(-rotationBackwardsSpeed * Time.deltaTime, 0, 0);
the 'else if' part works fine, but the if inside the if statement doesn't work at all.
The problem I see with your code is that when the rotation reaches 20
degrees, the if
is evaluated to false, and the else
block is ran, thus, the object rotates back to a value less than 20
and the if
is ran the next frame, the object rotates a little bit and the process repeats : else
is ran, if
is ran next frame, else
, if
..... again and again....
Your answer
Follow this Question
Related Questions
Quaternion to Matrix Conversion Error - Camera Script 0 Answers
rotate problem pls help 1 Answer
Position errors on rotated child objects? 0 Answers
Quaternion To Matrix conversion failed because input Quaternion is invalid 1 Answer
Gun Rotation 2 Answers