Reputation: 639
#pragma strict
var flare : GameObject;
var speed : float = 1;
function Start ()
{
flare = GameObject.FindGameObjectWithTag("flare");
}
function Update ()
{
var distance = Vector3.Distance(flare.transform.position, transform.position);
if (distance < 100)
{
Debug.Log ("Enemy is close to flare");
var delta = flare.transform.position - transform.position;
delta.Normalize();
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}
else
{
Debug.Log("Not close yet" + distance);
}
}
This is the script I have, when i right click on the mouse, it shoots out a flare, what I want to happen is for the enemy to go towards the flare when its active, at the moment, my enemy just ignores it. Any chance anyone knows why?
Any replies appreciated.
Upvotes: 0
Views: 135
Reputation: 1015
I'm not sure about Javascript -- but in C# ( which is similar ) you can make a few changes.
void Start( ){
flare = GameObject.FindObjectWithTag( "flare" ).transform;
}
void Update( ){
var distance = Vector3.Distance(flare.transform.position, transform.position);
if( distance < 100 ){
transform.position = Vector3.MoveTowards( transform.position,
flare.transform.position,
speed * Time.deltaTime );
} else {
//Do otherthings
}
}
Make sure that this script is either attached to the AI gameobect, or is referencing it ( I'm assuming from your script that the script is on the AI object ).
Upvotes: 1