Search Unity

How would I get the bottom of my player to face the direction I point

Discussion in 'Scripting' started by Armetron, Sep 4, 2018.

  1. Armetron

    Armetron

    Joined:
    Oct 7, 2016
    Posts:
    12
    Hello forum,

    I am trying to write a script where it will rotate the player so that its bottom will be pointing in a direction that I set without rotating it on its local Y axis.

    I still haven't fully grasped on how quaternions work, but from what I understand it's a directional axis that goes through the object's origin and rotates it along that axis by a set amount. I figured it would use a cross product of my players "down" and my direction to get the axis that I want to rotate around but after that everything I try just ends up glitching out

    I tried to use Quaternion.AngleAxis by giving it the cross product and the Vector3.Angle of both directions and that didn't work


    If anyone has a good place to understand Quaternions could you also link it in the comments
    as well
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,095
    You can use Quaternion.LookRotation to construct a rotation that faces a direction. You can also construct a rotation from euler angles with Quaternion.

    Best of all you can multiply them together:

    rotation = Quaternion.LookRotation(dir) * Quaternion.Euler(offset);

    So you don't have to be that good at Quaternions to use them effectively.
     
unityunity