- Home /
Erratic behaviour during rotation
After a one-week battle of reading the other topics on the forum and numerous tries to alter my code I need your help with defining a proper rotation logic. My problem is that, at a certain angle, the objects forget that they should rotate and stop in place until the player moves near them at a specific angle, as can be seen in the video. http://www.megafileupload.com/en/file/556113/Rotation-Flip-wmv.html
I know the answer is probably painfully obvious, but I just can't understand how to do it and I'm just too short a unityscript coder to grasp the Quaternion and eulerAngles, so if you could at least pinpoint in which direction I should go to understand this. I don't want to copy others script because it will put me nowhere in the long run.
Here is the code which I am currently trying to work:
var rotation = Quaternion.LookRotation(astTarget.position - astSelf.position);
astSelf.rotation = Quaternion.Slerp (astSelf.rotation, rotation, astSmoTur * Time.deltaTime);
astSelf.rotation.y = 0;
astSelf.rotation.x = 0;
And here is the additional code attached to the object:
function Update()
var newSpeed = asteroidSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, asteroidTarget.position, newSpeed);
transform.Translate (Vector3.right * newSpeed);
Answer by rutter · Aug 06, 2014 at 09:16 PM
This bit:
astSelf.rotation.y = 0;
astSelf.rotation.x = 0;
Most designers are used to working with "Euler angles", which is what you'll see in the inspector, but Unity's internals use quaternions to represent rotation. They're quite a bit more complicated, but do have some advantages I won't go into. You almost never want to edit a quaternion manually.
From the manual:
They are based on complex numbers and are not easy to understand intuitively. You almost never access or modify individual Quaternion components (x,y,z,w)
You can convert back and forth easily, though:
var euler = astSelf.eulerAngles;
euler.x = 0;
euler.y = 0;
astSelf = Quaternion.Euler(euler);
I have made an attempt to convert the previously provided code with quaternions into eulerAngles, but although the objects will no longer get themselves locked, their local (?) rotation alongside the Z axis will not take place at all - the objects will simply move towards the player alongside the x and y axes without turning around.
I guess it is too difficult for me, at least at this point, to figure out this concept. I just can't visualize this in my head and the Unity 3D manual doesn't help that much.