Now, all you programmers out there…I am giving you a challenge!!!![]()
I dare you to find the mistake that I have made in my code!!!
(I couldn’t find it myself lol)
The code is for a basic enemy AI. If the user is closer to the enemy than a particular value, he will get into “ready for battle” mode. If the user more closer, he will move towards the user (in the intention of attacking him). If he is dead close (closer than 1.5m) he will attack him.
Now when I run the script, the enemy is not moving at all!!! When he needs to move towards the user, he seems to turn towards the user and just stand there. He just doesn’t move. I just don’t know what to do. Perhaps some help here!?!?
Here is the code :
#pragma strict
var player : GameObject;
var SightDistance : float = 10;
var AttackDistance : float = 5; //this should ALWAYS be lesser than sight distance and greater than 1.5
var speed : float;
function Start () {
speed = 5;
animation.Play("idle");
}
function Update () {
if(Time.timeScale != 0)
{
var distance : float = Vector3.Distance(player.transform.position, transform.position);
if(distance <= SightDistance distance > AttackDistance)
{
animation.Play("waitingforbattle");
transform.LookAt(player.transform);
}
else if(distance <= AttackDistance distance > 1.5)
{
animation.Play("run");
speed = 7;
transform.LookAt(player.transform);
gameObject.GetComponent(CharacterController).SimpleMove(Vector3.forward * speed);
//transform.Translate(Vector3.forward * speed);
}
else if(distance >= 1.5)
{
speed = 0;
var time : int = 10;
if(time <= 0)
{
animation.Play("attack");
time = 10;
player.GetComponent(Player_Stats).removeHp(Random.Range(15.0f, 20.0f));
}
else
{
time--;
}
}
else if(distance < SightDistance)
{
transform.LookAt(player.transform);
transform.rotation.y += 180;
animation.Play("run");
speed = 7;
gameObject.GetComponent(CharacterController).SimpleMove(Vector3.forward * speed);
}
else
{
//walk randomly
}
}
}