Turret Will Not Follow Player

Hello all. I’m having a tough time with something very simple…

So I’m just trying to get a basic design done: get a turret to follow my player. The only objects in my scene are…

  1. A floor made of planes
  2. 1 cube
  3. 1 player controller (camera, etc)

I’ve made a cube and attached a script that should work. Here is the code.

using UnityEngine;
using System.Collections;

public class turretFollow : MonoBehaviour
{

    public Transform target;

     void OnTriggerStay(Collider col)
    {

        if (col.tag == "Player")
        {
            transform.LookAt (target);
        }
  
     }
}

The cube does indeed have a collider that is set to trigger. It is a box collider. I’ve made it wide and long, so that it’s view is large. Once I step into the box collider, it grabs only for a moment where I entered, and stays there. Once I get really close to the cube, then it starts to follow as intended. If I take a few steps back, it no longers follows me (even though I am still inside the collider box.)

My player controller is tagged as “Player”

The “target” variable has been checked in the inspector as the player controller

What is going on? :frowning: I couldn’t find any tutorials or other forum posts about this specific problem.

transform.LookAt(col.transform);

I tried your suggestion, but I still get the same results :frowning:

actually, your original code was fine.

I expect the trigger isnt firing. Put some debug.log statements in there.

I was able to solve the problem. Thank you all who took a look at this :slight_smile: