- Home /
Rotation of an object with quaternion
I have a problem rotating an object with quaternion, when I rotate the object I use the function slerp to rotate from one rotation to another, but in this process the object loses its local Y axis rotation, so I want to know how could this be avoided, the code that I use:
var orgRot = transform.rotation;
var myForward = Vector3.Cross(transform.right, normal);
var dstRot = Quaternion.LookRotation(myForward, normal);
for (var t: float = 0.0; t < 1.0; ){
t += Time.deltaTime*2;
transform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
yield;
}
There are a number of techniques for solving this kind of problem, but in order to suggest one, I'd need a firm understanding of 1) what rotation you are looking for, and 2) a better idea of how this code is failing you. Images are good. Video can be helpful.
$$anonymous$$y fault for not be more specified: the rotation that I am looking for it is the rotation of the player from the ground to a wall, the floor, an inclined surface, etc..., so this code is the part that calculates the rotation that has to be applied to the character from the previous rotation to the new rotation in the surface, which will be its new "ground" surface.
The problem: when the character is already in the new surface, he is not looking at the same angle in its y local axis that before, and I think that the problem has to be in this part of the code. Here some pictures:
Here is the character and behind him the wall which will become its new ground:
http://answers.unity3d.com/storage/attachments/33845-1.png
Now the character is floating, so the player can choose the surface to move the character, who is looking to the left in the picture:
And finally the character in the new surface, and now the character is looking forward, when actually he should be looking to the left ( which is forward in the third picture)
http://answers.unity3d.com/storage/temp/33848-3.png
$$anonymous$$aybe the explanation is not clear, if it is the case, I will upload a video to youtube
If I can getting a really clear idea of the rotation needed, I can almost always figure out the code. Here I'm still guessing. $$anonymous$$aybe you are looking for this:
var dstRot = Quaternion.FromToRotation(transform.up, normal) * transform.rotation;
That is exactly what I needed, now the character rotates from one surface to another correctly, and he makes a more natural movement, it is just I didn't undertand all the quaternion functions , thank you very much for the help robertbu
Answer by unimechanic · Oct 14, 2014 at 03:06 PM
I use the function slerp to rotate from one rotation to another, but in this process the object loses its local Y axis rotation
You can try rotating only in the axis that you need, check out this answer:
http://answers.unity3d.com/questions/246168/using-quaternionlookrotation-on-y-axis-only-but-ke.html
The game is like gravity rush, so you can walk in any surface by changing the gravity of the player (not literally the physics.gravity, but similar), I think that I can't rotate only in one axis, because sometimes, the rotations are in the 3 axis at the same time, because these rotations are made taking in acount the rotation of the new surface that will become the new ground of the player.
Your answer
Follow this Question
Related Questions
LookRotation Vector3 is Zero, Yet Slerp Still Rotates? 2 Answers
Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers
Rotation direction in coroutine 2 Answers
Quaternion slerp t parameter never reaches 1 1 Answer
Selecting the rotation direction for Quaternion.Slerp 1 Answer