Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

"LookAt" in the Y axis?

Discussion in 'Scripting' started by josecm, Dec 16, 2010.

  1. josecm

    josecm

    Joined:
    Mar 21, 2010
    Posts:
    96
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    You do that by looking at them while ignoring all y data in the function call. Like this:

    transform.LookAt(Vector3(otherObject.position.x, transform.position.y, otherObject.position.z));
     
  3. josecm

    josecm

    Joined:
    Mar 21, 2010
    Posts:
    96
    uauhh amazing!!!

    thanks you very much!!!
     
  4. helioraanswer

    helioraanswer

    Joined:
    Apr 7, 2009
    Posts:
    412
    Hello, I've used the code but my object shakes like hell.

    transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));

    If I use otherObject it doesn't look at my moving playable unit.

    And any idea how I can add that Quaternion Slerp to it to make it smoother?

    Thanks,
     
  5. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    If the object is jittering or shaking, try moving the call to LookAt() to LateUpdate() and see if that makes a difference.
     
  6. Warp boy2

    Warp boy2

    Joined:
    Nov 12, 2010
    Posts:
    216
    when i try.. it says position is not a member of vector 3...

    can someone help? does this script work on unity 3?


    actualy i want this script in a diferent way.. i want to use look at, but only with the Z and X axis, without looking at the Y
     
  7. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Post your script in its entirety; also, post the error message, and indicate (e.g. via comment) what line generates the error.

    Probably, but we don't know what script you're referring to.

    It sounds to me like you're looking for the same thing as the OP, but it's not entirely clear one way or the other. I'd recommend getting your current script working first though, as you may find it does in fact do what you want.
     
  8. Warp boy2

    Warp boy2

    Joined:
    Nov 12, 2010
    Posts:
    216
    okay, heres the part of the script that im having trouble (since the script itself is very big and redundant )

    Code (csharp):
    1.  
    2.  
    3. function OnTriggerEnter(other : Collider){
    4.  
    5. [COLOR="seagreen"]//(this right here is a statement to check the
    6. nearest object tagged with "aimable" once it finds it, the
    7. character will look at it )[/COLOR]
    8.  
    9. if(other.gameObject.tag == "aimable"  wasTriggered == false){
    10. mySphere.radius = 0;
    11. targets = other.gameObject;
    12. wasTriggered = true;
    13.        
    14. animation.Stop("subweapon1");
    15. animation.Play("subweapon2");
    16.        
    17. [COLOR="seagreen"]//(this "alvo" variable is the object the
    18. character has to look at.. but .. the problem is,
    19. when this object is waay above him.. he
    20. rotates its Y axis to look at it.. and it looks
    21. ridiculous... i wanted to make just the X or
    22. Z look at the target direction )[/COLOR]
    23.  
    24. var alvo = targets.transform.position;
    25.  
    26. [COLOR="seagreen"]//(now im using this transform.LookAt
    27. (alvo) because despite being ridiculous.. at least it works..)[/COLOR]
    28.  
    29. transform.LookAt (alvo) ;
    30.        
    31. var sub = Instantiate (misile,GameObject.Find("SUBWEAPONPOINT").transform.
    32. position,Quaternion.identity);
    33.        
    34. sub.transform.LookAt (alvo) ;
    35.  
    36. [COLOR="seagreen"]//(i also have the "sub" projectile looking at the
    37. object, but in this case,its ok to turn its Y axis..)[/COLOR]
    38.  
    39.     }
    40. }
    41.  
    42.  


    okay, basicly... this is my script up there.. the only diference was that i replaced that part with : transform.LookAt (alvo)

    by this :

    transform.LookAt(Vector3(alvo.position.x, transform.position.y, alvo.position.z));

    that was de script that i found here.. offcourse this would not fit to me because this script is specify to look at the Y axis.. but i was just testing if it work then i would do the necessary changes
     
    Last edited: Dec 26, 2010
  9. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    'alvo' is a Vector3, not a transform, so this:

    Code (csharp):
    1. transform.LookAt(Vector3(alvo.position.x, transform.position.y, alvo.position.z));
    Is incorrect (since Vector3 does not have a field or property named 'position').

    Try this instead:

    Code (csharp):
    1. transform.LookAt(Vector3(alvo.x, transform.position.y, alvo.z));
     
  10. Warp boy2

    Warp boy2

    Joined:
    Nov 12, 2010
    Posts:
    216
    Now it works just fine!.. thanks mr.anders!
     
    Typical_Ent likes this.
  11. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    558
    Do you get the error: Vector3 can not be used like a method? when using:

    transform.LookAt(Vector3(alvo.x, transform.position.y, alvo.z));
     
  12. Smingleigh

    Smingleigh

    Joined:
    Nov 24, 2012
    Posts:
    9
    Code (csharp):
    1. transform.LookAt(new Vector3(alvo.x, transform.position.y, alvo.z));
    The new keyword means you're creating a new instance of Vector3 with the stated arguments.
     
    Rianne-Schaidhauer likes this.