Help with Basic AI

Hi! I am starting to write AI code and I’m having some issues with a very simple AI.

The issue:

I want the enemy to stop as soon as he enters the maxDistance variable, so he cannot keep going forward. I think my code is right, but it doesn’t work and I can’t find the problem. Thank you! :smile:

The code:

private GameObject player;
public float distance;
private CharacterController thischar;
public float speed = 5.0f;
public Vector3 movementh = Vector3.zero;
public float chaseRange = 1.0f;
public float attackRange = 1.5f;
public float gravity = 20.0f;
public float lookatdistance = 20.0f;
public float dumping = 8.0f;
public float maxDistance = 0.4f;

// Use this for initialization
void Start () {

player = GameObject.FindWithTag (“Player”);
thischar = GetComponent ();
}

// Update is called once per frame
void Update () {
distance = Vector3.Distance (player.transform.position, transform.position);
movementh.y -= gravity * Time.deltaTime;
thischar.Move (movementh * Time.deltaTime);

if (distance <= chaseRange)
{
if (distance >= maxDistance)
{
movementh = transform.forward * speed;
print (“la distancia es mas grande que la maxima”);
}
else
{
movementh = Vector3.zero;
}

}
else
{

if (distance <= lookatdistance)
{
LookAt ();

}

}

}

void LookAt ()
{

Quaternion rotation = Quaternion.LookRotation (player.transform.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * dumping);
}

Code tags would work… :wink:

	// force the distance to clamp the movement.
	if(distance < maxDistance){
		Vector3 temp = new Vector3(movementh.x, 0, movementh.z);
		temp = temp.normalized * (distance - maxDistance);
		temp.y = movementh.y;
		movementh = temp;
	}


// Also, you should re adjust movementh every Update to make it equal to the velocity of the CharacterController
movementh = thischar.velocity;

Isn’t there an easier way to do it?

A trigger?

Oh, sorry, I think I told you wrong, but it is similar.

You must calculate the distance we are going to move before you move.

void Update () {
	float distance = (transform.position - player.transform.position).magnitude;
	movementh.y -= gravity * Time.deltaTime;
	
	// get the movement amount we are going to move
	float travelDistance = new Vector3(movementh.x, 0, movementh.z).magnitude * Time.deltaTime;
	float nextDistance = distance - travelDistance;
	
	// force the distance to clamp the movement.
	if(nextDistance  < maxDistance){
		Vector3 temp = new Vector3(movementh.x, 0, movementh.z);
		temp = temp.normalized * (nextDistance - maxDistance);
		temp.y = movementh.y;
		movementh = temp;
	}
	
	thischar.Move (movementh * Time.deltaTime);
	
	if (distance <= chaseRange){
		if (distance >= maxDistance){
			movementh = transform.forward * speed;
			print ("la distancia es mas grande que la maxima");
			
			movementh = Vector3.forward * speed;
			movementh.y = thischar.velocity;
		} else {
			movementh = Vector3.zero;
		}
		
	}
	if (distance <= lookatdistance){
		LookAt ();
	}
}

The other option is to move your minion backwards if he is too close, but that option allows the player to push the minion.

I fixed the code to calculate the next distance. and used that in the equation. I also added in the movers and such that make him start.