Search Unity

LookAt to rotate only on Y axis?

Discussion in 'Scripting' started by DaveyJJ, May 20, 2010.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Code (csharp):
    1. // The target variable shows up as a property in the inspector.
    2. // Drag another object onto it to make the camera look at it.
    3. var target : Transform;
    4.  
    5. // Rotate the unit every frame so it keeps looking at the target
    6. function Update() {
    7.     transform.LookAt(target);
    8. }
    It must be simple but it is me after all ... :oops:

    How do I make this transform happen only in the Y-axis, so that the object it's attached to only rotates around the Y axis to look at the target?
    Gracias.
     
  2. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Code (csharp):
    1. function Update() {
    2.     transform.LookAt(target.position.x, transform.position.y, target.position.z);
    3. }
    Though, you'll probably want to cache your local transform.
     
    Curipoc likes this.
  3. BrettFromLA

    BrettFromLA

    Joined:
    Jan 29, 2009
    Posts:
    244
    Wow burnumd, that's elegant code. I'm going to use that in the future. Thanks for posting!
     
  4. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Thanks much! I will tackle when I get home and get my laptop plugged back in again. 9% battery right now.

    Nope ... no luck ... this is the error ...

    No appropriate version of 'UnityEngine.Transform.LookAt' for the argument list '(float, float, float)' was found.
     
  5. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    anattress likes this.
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));
     
  7. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Thanks, that fixed it.
     
    Curipoc likes this.
  8. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    Aren't transform.position a Vector3?

    So you should be able to write
    Code (csharp):
    1. transform.LookAt(target.position);
     
  9. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    No it's a Quaternion... Haha just joking. Your code will billboard all the axes, and that's not what he wants (Read the title).
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Actually this DOES work and rotates only on Y.

    Reading Carefully (^_^) the code isn't (target, target, target), it's (target, THISTRANSFROM, target) which is why it works.

    The summation of the code should be:

    Code (csharp):
    1. var thisTransform : Transform;
    2. var target : Transform;
    3.  
    4. function Start/Awake () {
    5.   thisTransform = this.Transform;
    6. }
    7.  
    8. function Update() {
    9.     thisTransform.LookAt(Vector3(target.position.x, thisTransform.position.y, target.position.z));
    10. }
    What I'm finding with this code, is that is seems to monkey with the physics of the object that's using it, but this is probably an issue with LookAt(), rather than ignoring the Y.