- Home /
Make enemy follow flare
#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, it is attached to my enemy object. 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.
What object is this script on... Player, Enemy, or Flare?
If it's on the enemy then I don't see how the flare variable will be set, as the flare almost certainly doesn't exist when the Enemy is created.
If it's on the flare, then you are getting the flare to move towards the flare.
You need to do something like set a 'target' variable on the script of all enemies when a flare is created, rather than trying to set it when the enemy is created (if that's what you are doing). There are more efficient ways to do this but that should get things working.
Also, you can use Vetcor3.Lerp to help with the movement... it will be less lines of code than you have above.
Ok so the script is on your Enemy. Remove the line where you set 'flare' in Start(), and set it on all enemies when a flare is created (and delete it when it expires). If you have an 'enemy' tag you could get all Enemy scripts and set flare that way.
The problem may be how you are assigning flare but it's hard to say wihtout more information.
Is there a flare in the scene to begin with or is it instantiated from a prefab when the mouse is clicked?
Is it possible for there to be more than one flare in the scene at once?
Yes, there is a flare in the scene to begin with, just as a test. The enemy will move towards that flare. $$anonymous$$ore are instantiated when i use the mouse click, these show up as flare(Clone) and the enemy just ignores those ones but they are the ones i want the enemy to follow.
It is possible to have more than one as there is an inventory system that can hold upto 5
Answer by TheValar · Nov 06, 2013 at 05:45 PM
The problem is that flare is assigned only in the Start function so any flares not in the scene at the start are ignored. You need to re-assign this variable when a new flare is created. You may want to assign the flare variable in the update loop so that it's always looking for new flares.
Another problem is that GameObject.FindGameObjectWithTag("flare"); will only find one flare (not necessarily the one you want). You may want to GameObject.FindGameObjectsWithTag("flare"); to get a list of all the flares, then see which one is the closest and assign it to the flare variable.
This really isn't the best approach. You don't want to poll for flares being created every Update, you want to push this info out whenever a flare is created. If you want the closest flare to each enemy just do a distance check before updating the flare variable. Better still you could manage flare objects somewhere centrally (store a list of all active flares) so you can access them directly and add a listener callback for interested objects that need to know about creation or destruction of flares.
I agree completley with Huacanacha about efficiency. $$anonymous$$y way is simply the simplest implementation so that you could get the behavior working. It should certainly be optimized at some point as per Huacanacha's recommendations.
Answer by Huacanacha · Nov 06, 2013 at 05:43 PM
The flare that's already in your scene will work as it exists when the Start() function is called on the Enemy script. If you create clones in code the enemy has no way of knowing what these objects are as they are created after Start has run, so you need to figure out a way to tell the enemies a new flare has been created (See GameObject. FindGameObjectsWithTag to get all objects tagged 'enemy, for example).
Answer by JayFitz91 · Nov 06, 2013 at 06:12 PM
Brilliant, have it working now, thanks for the help lads,i appreciate it
You're welcome! Btw. thumbs up on any useful comments is appreciated.
This answer should really be a comment as well...
Your answer