Hey guys, I hope this is less of a mystery in the end, then it is to me.
Basically what I have is a Player, which gets rotated around his local x axis by a mouse script.
A second script rotates the player around the global axes, always 90 degree.
Now here is the problem: using transform.Rotate(eulerAngles, Space.World), I only get an incredibly small, unproblematic offset, so that the player gets almost perfectly aligned to the global axes in the end.
However, when I use itweens iTween.RotateAdd(), also in global space, I get a significantly bigger offset. This offset adds on, until the Player isn’t aligned anymore, practically not orthogonal anymore.
Errors that I have tested/disproved:
- It’s not caused by some sort of gravity or selfmade force. I’ve turned it off and made the attached rigidbody of the character kinematic.
- It’s not caused by mouse look. Even when not using mouse look, an offset occurs.
- It always gets passed 90 degree-rotations
Is there any known issue that could cause this kind of behaviour, or do you need some script and/or pictures?
EDIT:
Here’s the code, also posted in comments
public void switchGround()
{
//Some code to determine the normals which is pointed at with raycasting
Quaternion desRot = Quaternion.FromToRotation(transform.up, hit.normal);
float desRotX;
float desRotY;
float desRotZ;
//normalizing it to 180 degree max
if (desRot.eulerAngles.x > 180) { desRotX = desRot.eulerAngles.x - 360; }
else { desRotX = desRot.eulerAngles.x; }
if (desRot.eulerAngles.y > 180) { desRotY = desRot.eulerAngles.y - 360; }
else { desRotY = desRot.eulerAngles.y; }
if (desRot.eulerAngles.z > 180) { desRotZ = desRot.eulerAngles.z - 360; }
else { desRotZ = desRot.eulerAngles.z; }
Vector3 ea = new Vector3(desRotX, desRotY, desRotZ);
if (ea.magnitude < 178) //I want to handle 180 degree rotations manually
{
if (!rotating)
{
//clamping it to 90 degree rotations
if (Mathf.Ceil(desRotX) >= 45) desRotX = 90;
else if (Mathf.Floor(desRotX) <= -45) desRotX = -90;
else desRotX = 0;
if (Mathf.Ceil(desRotY) >= 45) desRotY = 90;
else if (Mathf.Floor(desRotY) <= -45) desRotY = -90;
else desRotY = 0;
if (Mathf.Ceil(desRotZ) >= 45) desRotZ = 90;
else if (Mathf.Floor(desRotZ) <= -45) {desRotZ = -90; }
else desRotZ = 0;
ea = new Vector3(desRotX, desRotY, desRotZ);
//transform.Rotate(ea, Space.World); <---- This works fine
iTween.RotateAdd(gameObject, iTween.Hash("amount", ea, "time", 0.4f, "space", Space.World, "onstart", "rotStart", "oncomplete", "rotComplete", "easetype", "easeInOutCubic"));
//This doesn't. onstart and oncomplete functions get called to make the rigidbody kinematic and stop gravity for the rotation
}
}
else
{
//What happens for 180 degree rotations, not important for the question
}
}
It is easiest for folks on the list to figure out these kinds of problems if we see the code.
– robertbuOkay, I hoped I could get away without posting it, since it's a rather long bit of code. Let me see if I can condense it down to the essential parts, then.
– PsychoHead- code edited in the first post -
– PsychoHead