Home > database >  how do i make an enemy look at a player only on the x axis in unity
how do i make an enemy look at a player only on the x axis in unity

Time:05-03

 public mouselook mouselook;
    public float speed = 20.0f;
    public float minDist = 1f;
    public Transform target;
    public Transform enemylocation;
    // Use this for initialization
    void Start()
    {
        // if no target specified, assume the player
        if (target == null)
        {
            if (GameObject.FindWithTag("Player") != null)
            {
                target = GameObject.FindWithTag("Player").GetComponent<Transform>();
            }
        }
    }

    // Update is called once per frame
    void Update()
    {


        if (target == null)
            return;
        // face the target
        enemylocation.LookAt(target);
        
       
    public void SetTarget(Transform newTarget)
    {
        target = newTarget;
    }

i looked online for about an hour and i tried implenting a vector 3 but was unable. can anyone please help? i want the enemy to look at the player(target) only on the x axis and z axis and not on the y axis due to it making it float. is there maybe also a way to make it look at the player - 10x or something like that?/ thanks in advance!

CodePudding user response:

I guess you just want to yaw the enemy, this can be achieved when the target is on the same level of the enemy.

var p = target.position;
p.y = enemylocation.position.y;
enemylocation.LookAt(p);
  • Related