Hi all,
I have been writing some code to check if the player has turned more than 45% since I will add a turn animation. I want to take in the player direction each frame into previousRot, then after the player has turned, check if the angle has changed by more than 45%. But my final debug.log is telling me that there is no change. Giving back a 0.
Here is my code:
previousRot.rotation = transform.rotation;
// Rotate player code here
if (transform.rotation.eulerAngles.y > previousRot.transform.rotation.eulerAngles.y + 45)
{
Debug.Log ("Player Turned")
}
Debug.Log (transform.rotation.eulerAngles.y - previousRot.transform.rotation.eulerAngles.y);
Any ideas what is wrong?
I’m assuming this is all done in Update. If so you appear to be setting the previous rotation to the current rotation each frame so therefore they would always be equal. Put a breakpoint on your if statement & check the angles each frame just to confirm it though.
It looks like previousRot is declared as a Transform or GameObject, so you’re comparing a transform against itself which will always be the same value. You want it to be a Quaternion.
My logic is that I set them the same, then the rotate player code happens and transform.rotation changes but previousRot does not. Then I compare them… for each frame. Is that part of it wrong?
You need to ensure you only set them the same once, not each frame.
ok, did that. they are still outputting the same every frame though?
Not if previousRot is a Transform or GameObject because of how reference types work. You’re literally comparing the same value every frame because you’re not actually storing the old value like you think you are.
Ok, and excuse my ignorance, but my next question now has to be, how do I pass a transform.rotation into a Quaternion?
Transform.rotation is a Quaternion.
Up at the top of your code, change the line that looks like this:
Transform previousRot;
To this:
Quaternion previousRot;
And change your code to accommodate the change:
previousRot= transform.rotation;
// Rotate player code here
if (transform.rotation.eulerAngles.y > previousRot.eulerAngles.y + 45)
{
Debug.Log ("Player Turned")
}
Debug.Log (transform.rotation.eulerAngles.y - previousRot.eulerAngles.y);
Thanks very much. That works and I can compare the values. The passing of previousRot= transform.rotation;
DOES need to be in update, as I first thought, or previousRot just returns 0.
This if statement I will have to think over the maths, because it doesn’t always return a “player turned” when turning more than 45%. I think because after 360 is back to 0.
if (transform.rotation.eulerAngles.y > previousRot.eulerAngles.y + 45)
Spent many hours thinking I would be smart enough to work this out… I’m not.
So how can I make a comparison to find out if the player has turned over 45% in either direction, even if they go over 360? What clever maths needs to be put in the condition?
Thanks.
Looking at it from the other side, I think finding the angle turned by the player as a first step is best.
In this case, which kind of compare angle function would work with what I have?