Search Unity

Quaternion not producing proper rotation

Discussion in 'Scripting' started by Iron-Warrior, Feb 24, 2010.

  1. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Yeah, I know, my last post is like 3 below this. Promise not to post a new thread for a week. But I've been working for an hour and a half straight and there's no way I can figure this out.

    Making an FPS game, making climbable walls, aligning the player to face the wall turning into a massive problem. After much hair pulling I decided to get down to the basics and see if I was having issues with quaternions. Which I was.

    Code (csharp):
    1.         var normalHit = Quaternion.FromToRotation(Vector3.up, hit.normal);
    2.         stupidTest.rotation = normalHit
    After searching the forums a bit, I found that this should make my transform, stupidTest, have the exact same rotation as normalHit, right? Well it's not working!

    So I eventually made a dummy object (stupidTest) that matches the rotation of an object I come into collision with (this was originally for my function that lets players climb ladders).

    Here is the before (the dummy is the exact same object as the walls, just submerged in the ground halfway)



    Okay, so when I collide (poke) with the wall, this wall should turn 45 degrees to the right...correct?

    Well, no.



    Notice that I have the debugger open. Using

    Code (csharp):
    1. Debug.Log(Quaternion.FromToRotation(Vector3.up, hit.normal));
    I am displaying the rotation of the object I am colliding with as a quaternion. After I collide with the first object, (-0.5, 0.0, -0.5, 0.7) pops up. When I collide with the dummy object AFTER colliding with the first wall (and it has assumed the new rotation) I get the same thing!.

    I don't really get quaternions, but if they have the same rotation, are the same object, they should be at the exact same orientation, right?

    Again, sorry for posting twice in one day, makes me feel like a ubar noob, but this was frustrating me to no end...any help would be great.[/i]
     
  2. FenrirWolf

    FenrirWolf

    Joined:
    Feb 10, 2010
    Posts:
    37
    Sorry, it's really hard to tell what's going on from your pictures and description.

    What is normalHit supposed to represent? Here is what I see from your code segment:

    Give me a rotation which moves from world up (Y+ axis) to face the direction of hit.normal.

    I don't think that is what you intend. You said you want to align the player so he is facing the wall, right? So, you want to rotate the player's forward axis so that it points exactly away from the wall's normal. (Note this assumes that your player's front axis is Z+, which is standard for Unity.)

    In other words:

    Code (csharp):
    1. newRotation = Quaternion.FromToRotation (player.transform.forward, -hit.normal) ;
     
  3. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    You are right...that wasn't quite what I was intending.

    Went back to it, looked at your idea, put it in and it works. Tested some stuff around and I think I'm getting the hang of quaternions now, they're just a little foreign.

    Thanks very much for the answer, this board is really great!
     
  4. FenrirWolf

    FenrirWolf

    Joined:
    Feb 10, 2010
    Posts:
    37
    No problem. And yeah, quaternions (or rotations in general) trip up a lot of people at first. I find that it's best to think about what I want to do in vector math initially and ignore quaternions, and then translate that into rotations when needed, much as what was done above.