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


    Oct 7, 2016
    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


    Digital Ape Moderator

    Apr 11, 2010
    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.