Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LookAt but only in Y

Discussion in 'Scripting' started by mediashock, Mar 15, 2011.

  1. mediashock

    mediashock

    Joined:
    Apr 15, 2009
    Posts:
    65
    Hi,
    I have this script,

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FlagLookAtMe : MonoBehaviour {
    5.     public Transform target;
    6.     void Update() {
    7.         transform.LookAt(target);
    8.  
    9.     }
    10. }
    It works good, but my object should ONLY rotate in Y value according to the Target, (which is the camera) if my camera is above the object, the object rotates on X or Z axis. Is there a way to just lock this to the Y axis?
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FlagLookAtMe : MonoBehaviour {
    6.     public Transform target;
    7.     void Update() {
    8.         Vector3 targetPos = target.position;
    9.         targetPos.y = transform.position.y;
    10.         transform.LookAt(targetPos);
    11.  
    12.     }
    13. }
    14.  
     
  3. mediashock

    mediashock

    Joined:
    Apr 15, 2009
    Posts:
    65
    oh i feel dumb, thanks legend411
     
  4. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Haha don't feel dumb! No problem.
     
  5. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    -Nevermind-
     
    Last edited: Apr 27, 2011