Script not working (530726)

Now, all you programmers out there…I am giving you a challenge!!!:wink:
I dare you to find the mistake that I have made in my code!!!:smile: (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
}
}
}

I believe it’s the order of your checks. That first check will almost always pass, which then causes all of the following checks to be skipped because they’re in ‘else’ clauses.

To best find errors, put yourself in the place of your CPU, step through each line, and actually do what each individual instruction says.

Pretty much like anrypenguin said:

        if(distance <= SightDistance  distance > AttackDistance)
        {
            animation.Play("waitingforbattle");
            transform.LookAt(player.transform);
        }

This tells the code to look at your player, but there is nowhere else telling it to actually move anywhere? That is why you see the unit face your player but not move. Also, a lot of your checks are actually True at any one time and it’s just the order of execution that stops them from all running each frame.

Thanks for the reply guys but I don’t think the if statement has a problem as that is evident from the animations that the enemy show.
The enemy shows all the animations as expected. Yet he just doesn’t move.
What else could the problem be?
Is it possible that the character controller is wrongly configured?
I have the default set for it and did not change anything (except for the height and radius).
Any other mistake you can think of?
Perhaps something similar you faced in the past??