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.

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 Maybe 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. Maybe 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

1 Answer

1

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:

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.