I am trying to create a follow bot type character that will follow the player when the player enters a collider.
This is what I have so far…
using UnityEngine;
using System.Collections;
public class FollowBot : MonoBehaviour {
Transform Player;
public float AISpeed = 10;
public float MinDistance = 2;
// Use this for initialization
void Start () {
Player = GameObject.FindGameObjectWithTag ("Player").transform;
}
void OnTriggerEnter(Collider obj) {
if(gameObject.tag ==("Player")){
if (Vector3.Distance(transform.position, Player.position) >= MinDistance){
transform.position += transform.forward*AISpeed*Time.deltaTime;
}
}
}
}
It doesn’t seem to work at all at the moment but I’m not sure why. It was working previously but when I modified it to make the follow bot follow the player when they enter the collider it stopped working. I can’t remember what I changed now because I tried multiple things.
If someone could please help me out that would be greatly appreciated. Thank you.
When the player enters the trigger, you are checking that the object that the script is attatched to has the tag Player, not the object entering the trigger. Instead of gameObject.tag, try using obj.tag.