- Home /
Euler Angles Rotation Problems
I have a code that is supposed to rotate an object towards another object on one axis, the code works the object is forced to turn more than 180 degrees, as if it can't fully turn around which is my only problem. I really want to keep the interpolation so that rotation is smooth. There is more to the code but here is the part where all the magic happens.
rotation = Quaternion.LookRotation(transform.position - target.transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotSpeed * Time.deltaTime);
var eulerAngles = transform.rotation.eulerAngles;
eulerAngles.y = startRot.eulerAngles.y;
eulerAngles.z = startRot.eulerAngles.z;
// Set the altered rotation back
transform.rotation = Quaternion.Euler(eulerAngles);
So what I am doing is creating a look rotation then slerping the object between current rotation and desired rotation, there is no problem there, then I get the euler angles of that altered rotation, then the y and z components of the euler angles are set to the same as the rotation at the start of the scene (the default rotation), therefore the other two axes will be reset (I know the documentation says don't alter the axes separately but this does't seem to be the cause of the problem). Then set the rotation back, but only for the y and z axes while the x axis remains the way it was when it was "slerped". Again the problem is that the object stops rotating once the target moves beyond the starting rotation (view), anyone know how to fix this?
Transform.Rotate seems to be made for rotating an object around one specific axis. You can check the angle to deter$$anonymous$$ the speed and Vector3.cross to deter$$anonymous$$ the direction.
It is strange that you are confining your rotation to the X axis. Typical games confine the Y rotation, What is the viewpoint of your game? And are their any constraints on the target?
With the z axis facing the camera and what exactly do you mean by constraints? $$anonymous$$ost of the time the target is the player.
Answer by robertbu · Mar 04, 2013 at 04:04 AM
If I correctly understand what you are trying to do, here is a bit of alternate code that replaces all the code above and gets you out of getting and setting Euler angles:
Vector3 pos = transform.position - target.transform.position;
pos.x = transform.position.x;
rotation = Quaternion.LookRotation(pos);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotSpeed * Time.deltaTime);
The primary problem is that the object rotates on other axes to the point where you can no longer see it (it's a 2d sprite), with the code I had it just can't rotate all the way around for some reason, it gets to a certain point and stops and starts jittering. The code above did not work, the sprite did the full rotation but you couldn't see it (because it's a plane), which if my euler angles code wasn't in my code the same thing would've happened. I think it's because the starting rotation angles are facing one way when the sprite is trying to turn around and face another direction, but I can't figure out how to resolve this.
Consider separating the rotation code and facing problem. You can solve the facing problem in a couple of ways. One way would be to parent the sprite to an empty game object (which gets the rotation code) and then add a sibling sprite that faces the opposite way (i.e. back to back sprites). Another way would be to make the sprite a child of the object that is doing the rotation. Have the sprite check it forward vector (Vector3.Angle()) against the camera's forward vector. If it faces away do a localRotation to flip/rotate the sprite to face the camera.
I took your first idea idea and it worked, however I simplified it a bit and ins$$anonymous$$d of having a separate game object I just altered the start rotation variable's x component based on wherever the player's x position was relative to the x position of the object itself. There's is a slight jitter when doing the full rotation (when the rotation is at 90 or 180 degrees) which can be fixed easily. Thanks!
If the problem is solved, can you please mark the question as answered?