I got an enemy that follows the player and explodes on impact (Creeper from MineCraft).
Now the problem is that he follows the player as soon as the game starts, I want him to only follow the player when the player is close to him.
I tried to use an invisible box for the detection area but it didn’t work.
I get no errors, and I get no “detected!” print.
var target : Transform;
var moveSpeed = 5;
var rotationSpeed = 5;
var myTransform : Transform;
var Detect : boolean = false;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
}
function OnTriggerEnter(other : Collider)
{
if (other.tag == "Player")
{
Detect = true;
print ("detected!");
}
}
function Update () {
if (Detect == true)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
also, If you got some other method I can use other than collision, please tell me.