Why is my AI not thinking the collisions fast enough?

Hi guys,

I’m currently working on a project of mine and I’ve seem to be having a dilemma.

Briefing of the project: it has 1 AI character (currently a sphere) and 4 walls.

What I am trying to achieve: for starters it would be nice if my AI wouldn’t go through the wall.

How does it move: My AI script makes it select a number from a random range, and depending on the number chosen it will change its direction in 2D, vertical, horizontal and diagonal.

What works: At the beginning I’ve only had 4 box colliders arranged perfectly so that it wouldn’t get out but it still got out due to the code sending it in an impossible area. I’ve managed to correct this by applying 4 invisible colliders on each of the walls - total of 16 colliders and each will limit the movement to the specific areas.

What doesn’t work: I am now trying to instantiate other walls to close in on the AI to test it, and it seems that at several points it still goes through the walls.

Question: How do I make it stop passing through the walls? Should I limit the x and y position of the AI or is there a simpler way?

If sample code is needed please ask!

Kind regards -
Eugen

Edit:
Note that the move_dir variable is a float.

function Start () {

	move_dir = Random.Range(-2,6);

}

function Update () {

	Actions();
}

function Actions () {

	if(move_dir == -2){
	
		cow.transform.position.x = cow.transform.position.x - 0.05;
		Debug.Log("Este -2");
	}
	
	if(move_dir == -1){
	
		cow.transform.position.x = cow.transform.position.x + 0.05;
		Debug.Log("Este -1");
	
	}
	
	if(move_dir == 0){
	
		cow.transform.position.y = cow.transform.position.y - 0.05;
		Debug.Log("Este 0");
		
	}
	
	if(move_dir == 1){
	
		cow.transform.position.y = cow.transform.position.y + 0.05;
		Debug.Log("Este 1");
	
	}
	
	if(move_dir == 2){
	
		cow.transform.position.x = cow.transform.position.x - 0.05;
		cow.transform.position.y = cow.transform.position.y - 0.05;
		Debug.Log("Este 2");
	
	}
	
	if(move_dir == 3){
	
		cow.transform.position.x = cow.transform.position.x + 0.05;
		cow.transform.position.y = cow.transform.position.y + 0.05;
		Debug.Log("Este 3");
	
	}
	
	if(move_dir == 4){
	
		cow.transform.position.x = cow.transform.position.x + 0.05;
		cow.transform.position.y = cow.transform.position.y - 0.05;
		Debug.Log("Este 4");
	
	}
	
	if(move_dir == 5){
	
		cow.transform.position.x = cow.transform.position.x - 0.05;
		cow.transform.position.y = cow.transform.position.y + 0.05;
		Debug.Log("Este 3");
	
	}
	
}



function OnTriggerEnter (hit : Collider) {
		
		//Collision with Right Wall
			
		if(hit.gameObject.tag == "Right Wall 1"){
		
			move_dir = RW_random[Random.Range(0, (RW_random.length))];
				
			Debug.Log("It's turned to" + move_dir);
			
		}
			
		if(hit.gameObject.tag == "Right Wall 2" || hit.gameObject.tag == "Right Wall 3"){
					
			move_dir = RW_random1[Random.Range(0, (RW_random1.length))];
					
			Debug.Log("It's turned to" + move_dir);
			
		}
		
		if(hit.gameObject.tag == "Right Wall 4"){
		
			move_dir = RW_random2[Random.Range(0, (RW_random2.length))];
			
			Debug.Log("It's turned to" + move_dir);
			
		}
			
		//Collision with Left Wall
			
		if(hit.gameObject.tag == "Left Wall 1"){
		
			move_dir = LW_random[Random.Range(0, (LW_random.length))];
					
			Debug.Log("It's turned to" + move_dir);
				
		}
				
		if(hit.gameObject.tag == "Left Wall 2" || hit.gameObject.tag == "Left Wall 3"){
			
			move_dir = LW_random1[Random.Range(0, (LW_random1.length))];
					
			Debug.Log("It's turned to" + move_dir);
			
		}
			
		if(hit.gameObject.tag == "Left Wall 4"){
	
			move_dir = LW_random2[Random.Range(0, (LW_random2.length))];
					
			Debug.Log("It's turned to" + move_dir);
			
		}
			
		//Collision with Top Wall
	
		if(hit.gameObject.tag == "Top Wall 1"){
		
			move_dir = TW_random[Random.Range(0, (TW_random.length))];
					
			Debug.Log("It's turned to" + move_dir);
			
		}
		
		if(hit.gameObject.tag == "Top Wall 2" || hit.gameObject.tag == "Top Wall 3"){

			move_dir = TW_random1[Random.Range(0, (TW_random1.length))];
					
			Debug.Log("It's turned to" + move_dir);
				
		}
		
		if(hit.gameObject.tag == "Top Wall 4"){
					
			move_dir = TW_random2[Random.Range(0, (TW_random2.length))];
					
			Debug.Log("It's turned to" + move_dir);
				
		}
			
		//Collision with Bottom Wall
			
		if(hit.gameObject.tag == "Bot Wall 1"){
		
			move_dir = BW_random[Random.Range(0, (BW_random.length))];
					
			Debug.Log("It's turned to" + move_dir);
			
		}
		
		if(hit.gameObject.tag == "Bot Wall 2" || hit.gameObject.tag == "Bot Wall 3"){

			move_dir = BW_random1[Random.Range(0, (BW_random1.length))];
					
			Debug.Log("It's turned to" + move_dir);
				
		}
		
		if(hit.gameObject.tag == "Bot Wall 4"){

			move_dir = BW_random2[Random.Range(0, (BW_random2.length))];
			
			Debug.Log("It's turned to" + move_dir);
				
		}
		
}

Code might be helpful - how are you moving this thing? CharacterController, position update, physics?

Code added. See Edit:.

Quick question, why do you have all your update stuff in the corutine actions? Why not just put everything in actions in update?

Already have that set up on the AI...still passes through the walls when i close up on it. (also sorry for the bump - forgot about the rule)

@negagames, I've done this because i'm thinking of going even further with the functions and I won't be able to keep up with the huge pieces of code in the Update function if I don't separate them like this. Plus I think it's more efficient this way, or am I mistaking?

1 Answer

1

I’ve seen this issue before. physics doesn’t always work if you move objects with colliders from the Update() method. You need to use FixedUpdate().

hope this helps,

Mike

Yup, that fixed my problem. All of you please move some of your comments as answers so I can choose them as correct as well as most are going to help me in further developing this app :)