- Home /
MainCharacter Enemy Following Script Problem
So, I'm making a FPS, in this game it's supposed that my enemies should look, follow and shoot me, however, none of this happens, here's my Enemy's Script's Update:
void Update ()
{
TimingAtacar++;
if (Vector3.Distance(this.transform.position, Jugador.transform.position) < DistanciaParaAtacar)
{
transform.LookAt(Camera.main.transform.position);
transform.position += transform.forward * Time.deltaTime * Velocidad;
if(TimingAtacar<=120)
{
Instantiate (BalaEnemigo, new Vector3 ( Enemigo.transform.position.x,Enemigo.transform.position.y,Enemigo.transform.position.z), Quaternion.identity);
TimingAtacar=0;
}
}
}
public float Vida = 200;
public void TakeDamage(float damage)
{
Vida -= damage;
if (Vida <= 0)
{
GameObject.Destroy(Enemigo.gameObject);
}
}
So some of that looks just fine, I'll make a couple of comments and see where we get to.
Velocidad is not 0 right? $$anonymous$$ake sure in the inspector.
DistanciaParaAtacar is a reasonable size, as in they should start moving at some reasonable distance (not 0 again of course)?
I'm guessing BalaEnemigo is the enemies bullet - you could just instantiate that without making the new Vector3 and use Enemigo.transform.position ins$$anonymous$$d by the way.
I'm presu$$anonymous$$g bullets know how to move? That they can't collide with the enemy as they start right on top of him...
One note - you should really make Ti$$anonymous$$gAtacar a float and do
Ti$$anonymous$$gAtacar += Time.deltaTime;
Then treat it as a value in seconds - that way your game plays the same logic irrespective of frame rate.
Thanks for the tip of deltaTime, Velocidad is not 0, DistanciaParaAtacar is 20, the bullets and the enemies are in different layers, and they should move, I have another question open about the fact that they dont.
Answer by Albano.conte · May 27, 2012 at 11:49 PM
Okay, I solved it,
transform.LookAt(Camera.main.transform.position);
transform.position += transform.forward * Time.deltaTime * Velocidad;
This should go Outside of the if that deals with the distance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072025im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bug with my enemyAI 2 Answers
MonoDevelop doesn't open script and keeps restarting? 0 Answers
Enemy following Player on uneven surface 1 Answer
Elevator script problem (Java) 1 Answer