Search Unity

Locking X and Z axis in the "transform.LookAt" command? (making NPC face player)

Discussion in 'Scripting' started by rhianu, Jan 23, 2011.

  1. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Hi, I'm trying to make an NPC turn to face the player whenever the player talks to the NPC, and I got it to sort of work by using transform.LookAt(targetPlayer); but the problem occurs when I have an NPC that is taller than the player, and the NPC rotates along the Z axis, causing it to lean forward and make its feet come off the ground so it looks like its floating in the air. I'm a newbie at coding, so don't I don't know how to fix this (looked up a bunch of stuff using the scripting reference, but couldn't make heads or tails of any of it), but basically all I need is a way to "lock" the X and Z axis, so that the NPC only rotates along the Y axis when turning to face the player.

    Any help would be greatly appreciated. Thanks.
     
    Last edited: Jan 23, 2011
  2. Afisicos

    Afisicos

    Joined:
    Nov 13, 2010
    Posts:
    326
    Try this:

    in function Update...

    transform.LookAt(targetPlayer);
    transform.rotation.x = 0;
    transform.rotation.z = 0;
     
    Last edited: Jan 23, 2011
  3. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Or using the previous example, just use the y component.
     
  4. sccrstud92

    sccrstud92

    Joined:
    Jan 20, 2011
    Posts:
    145
    You could also set the target to an empty thats parented to the NPC at head height, so when you LookAt it, the transform only needs to rotate around the y axis.
     
  5. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Awesome! Thanks, Afisicos! That worked great! (Although I had to use rotation instead of rotate like you typed, but whatever, it works).

    And TheRaider, I tried doing what you suggested, but I couldn't get it to work. I guess I just don't know the syntax well enough.

    One other question: now that I've got the NPC to face the player, how would I go about making the NPC rotate slowly, so you can actually see him rotating? Right now the NPC just instantly "pops" into the correct facing direction, and I'd rather the player be able to actually see the NPC turn.
     
  6. Afisicos

    Afisicos

    Joined:
    Nov 13, 2010
    Posts:
    326
  7. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Hmmm, looking through the info there, it seems like what I want would be the FromToRotation, but once again the proper syntax to do what I want eludes me. :(

    Also, as a side question, how would I make the NPC rotate back to its original position once the player was done talking to it?
     
    Last edited: Jan 23, 2011
  8. Sputnicker

    Sputnicker

    Joined:
    Mar 23, 2010
    Posts:
    26
    Here is an exemple of quaternion rotation, where the object looks directly to the mouse. I think it may help...

    Code (csharp):
    1. function Update () {
    2.     // Generate a plane that intersects the transform's position with an upwards normal.
    3.     var playerPlane = new Plane(Vector3.forward, transform.position);
    4.    
    5.     // Generate a ray from the cursor position
    6.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    7.    
    8.     // Determine the point where the cursor ray intersects the plane.
    9.     // This will be the point that the object must look towards to be looking at the mouse.
    10.     // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
    11.     //   then find the point along that ray that meets that distance.  This will be the point
    12.     //   to look at.
    13.     var hitdist = 0.0;
    14.     // If the ray is parallel to the plane, Raycast will return false.
    15.     if (playerPlane.Raycast (ray, hitdist)) {
    16.         // Get the point along the ray that hits the calculated distance.
    17.         var targetPoint = ray.GetPoint(hitdist);
    18.        
    19.         // Determine the target rotation.  This is the rotation if the transform looks at the target point.
    20.         var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    21.         transform.rotation = targetRotation;
    22.     }
    23. }