Problem is outdated
problem with stopping rotation!
Hey Guy's! I've got a little problem and I have no idea what I did wrong. I'm making a 2D game and at the moment I'm trying to implement that the player is rotating 90 Quaternions. Now that itself is not a problem. The problem is stopping at the right time. It should stop when rotation.y gets lower than 270 Quaternions, but it just continues and stops the rotation when it is just over 0.1 Quaternion... There's no problem with rotating it to 0.0 (so that shifted = false again). I honestly have no idea where my mistake is, so I would like to ask if you can find it. The script is written in C# Here's the code:
float rotationSpeed = Time.deltaTime * 50.0f;
if (Input.GetButtonDown ("Shift"))
{
shifting = true;
}
if (shifting == true)
{
if (shifted == false)
{
if (player.transform.rotation.y > 270.0f || player.transform.rotation.y < 0.1f)
{
player.transform.Rotate (new Vector3 (0,-rotationSpeed, 0));
}
else
{
Debug.Log(player.transform.rotation.y);
player.transform.rotation = Quaternion.Euler (new Vector3 (player.transform.rotation.x, 270.0f, player.transform.rotation.z));
shifted = true;
shifting = false;
}
}
else
{
if (player.transform.rotation.y > 0.0f)
{
player.transform.Rotate (new Vector3 (0, rotationSpeed, 0));
}
else
{
player.transform.rotation = Quaternion.Euler (new Vector3 (player.transform.rotation.x, 0.0f, player.transform.rotation.z));
shifted = false;
shifting = false;
}
}
}
I hope you can find the mistake and help me. Thanks in advance and have a nice day ^^
Answer by M-Hanssen · May 24, 2016 at 08:43 AM
The following: player.transform.rotation.y
is not measured in degrees angle but in quaternions.
Please read http://docs.unity3d.com/ScriptReference/Quaternion.html to gain a better understanding of this entity.
The second problem you are facing is that euler angels don't work very well with values past 90 degrees, both positive and negative. But since you restrict yourself to -90 degrees, the following could work:
if (player.transform.eulerAngles.y > -90 || player.transform.eulerAngles.y < 0.1f)
{
player.transform.Rotate(new Vector3(0, -rotationSpeed, 0));
}
Still this is not a solid solution.
Just tried it. Unfortunately it didn't work. It just spun indefinitely ins$$anonymous$$d of stopping after a full circle like it did before. Thank you anyway for the information regarding the unit, I will definitely read it, but I don't think that is a big issue in this case, since I just wrote degrees here, because I didn't know better. In the code they are still treated as Quaternions, otherwise the rotation back to 0 wouldn't work either, I'd say ^^
I didn't check your code for helping you reach your goal because I don't know what that is. $$anonymous$$aybe you can describe it in somewhat more detail.
I only pointed out that this will never work player.transform.rotation.y > 270.0f
The goal is to rotate the player 90 degrees (or quaternions) in the negative direction when shift is hit and then 90 degrees in the positive direction when it is hit again. I just use the player.transform.rotation.y to check how far the game object has been rotated and when it rotates in the positive direction it works just fine, so I don't know why it will never work ^^
Follow this Question
Related Questions
I want to stop movement of character while i call animation like Kick Jump Punch etc.. Please help 1 Answer
How to move an object on a 2d plane forward, backward, and in a rotation about the mouse position? 0 Answers
moving an object continuously between waypoints 0 Answers
Using quaternions to align game objects. 1 Answer
Ambient Emitter inside a BoxCollider - Rotation not recognized 0 Answers