Search Unity

Question rotating the bottom of an object to face another player.

Discussion in 'Scripting' started by puddleglum, Feb 1, 2023.

  1. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    412
    i have a script that i use in 2d to rotate something to face the player, and i adapted it to 3d, but in 2d whenever i wanted to change how it was rotated i just changed the direction of the sprite in photoshop, in 3d i cant do that and idk how to get a specific part of the object to be facing the target, for instance i want the player to be able to walk on walls so to orient them i would rotate them to face the wall, but their feet need to be facing the wall, how would i adapt this script to do that
    Code (csharp):
    1.  
    2.  Vector3 Direction = target.transform.position - transform.position;
    3.         Quaternion rotation = Quaternion.LookRotation(Direction);
    4.         transform.rotation = rotation;
    5.  
     
    Last edited: Feb 1, 2023
  2. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    if you want the bottom to face something, you could do:
    Code (CSharp):
    1. tranform.up = -directionBottomShouldFace
    problem is that it only guarantees one axis. you probably want something like
    Code (CSharp):
    1. transform.LookAt(directionToLookIn, -directionForFeetToPoint)
    https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
     
  3. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    412
    i didnt even know transform.lookat was a thing lol i probably should have, when experimenting with this i get a similar result, along with some weird results, i tried vector positons such as up down left right forward back, a physical world location, and a local position for the feet to be here is the code i have now
    Code (csharp):
    1.  
    2.   Vector3 Direction = CurrentPlanet.transform.position - transform.position;
    3.        // Vector3 FeetPosDir = FeetPos.transform.position - transform.position;
    4.        //Quaternion rotation = Quaternion.LookRotation(Direction);
    5.         //  tempRotat.rotation = rotation;
    6.         transform.LookAt(Direction, CurrentPlanet.transform.position
    7.  
    and here is the result

    im trying to get the players feet to be facing the surface of the planet, i added spheres to easier see the rotation of the player
     
  4. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    so your "Direction" variable seems to be your "gravity direction", presumably the direction the feet should face, presumably the down direction of the player (the opposite of the players y-axis). if so, then -Direction (NEGATIVE Direction, as in -1f * Direction) should be then SECOND parameter of the LookAt function. for the first parameter, i cant say what it should be, but its the direction you want the players z-axis to align to.

    keep in mind that if the 2 Vector3 params you provide as arguements are not perpendicular, then the "up" will be a bit off. For your case, it seems that this would be unacceptable, so you should make sure to give provide perpendicular vectors.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Then you want:

    Code (CSharp):
    1. Vector3 up = transform.position - CurrentPlanet.transform.position;
    2. Vector3 projectedForward = Vector3.ProjectOnPlane(transform.forward, up);
    3. transform.rotation = Quaternion.LookRotation(projectedForward, up);
     
  6. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    412
    ooh thankyou this makes a bit more sense, im gonna have to work on it a bit more to understand fully as im very very new to quaternions, this is my first 3d game :p
     
  7. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    412
    this actually really helps, being able to visualize it in code