Hey!
I am working on an AI script, trying different things and messing around.
I am working on trying to make the enemy chase the player and at the same time prioritize collision. The enemy should chase the player but when encountering an collision move around it and then continue the chase.
if(GUITest.taBortOk == true && GUITest.pausKnapp == false && GUITest.menuUp == false){
//MOVE AROUND COLLISION
if(Physics.Raycast(transform.position, transform.forward, hitRay, 25)){
if(hitRay.transform.tag == "TerrainCollision" || hitRay.transform.tag == "Target" || hitRay.transform.tag == "Moveable"
|| hitRay.transform.tag == "Wall"){
//if there is only a turn to the right
if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){
if(hitRay.transform.tag == null){
if(Physics.Raycast(transform.position, transform.right * -1, hitRay, 15)){
if(hitRay.transform.tag != null){
transform.Rotate(0, Time.deltaTime * 10 + 25, 0); //turn right
if(chaseIsOn == true){ //when there is nothing in the way anymore - continue chase if chasing
follow();
}
}
}
}
}
//if there is only a turn to the left
if(Physics.Raycast(transform.position, transform.right * -1, hitRay, 15)){
if(hitRay.transform.tag == null){
if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){
if(hitRay.transform.tag != null){
transform.Rotate(0, Time.deltaTime * -10 -25, 0); //turn left
if(chaseIsOn == true){ //when there is nothing in the way anymore - continue chase if chasing
follow();
}}}}}
//if both left and right ways are viable
if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){
if(hitRay.transform.tag == null){
if(Physics.Raycast(transform.position, transform.right * -1, hitRay, 15)){
if(hitRay.transform.tag == null){
transform.Rotate(0, Time.deltaTime * 10, 0); //turn right slowly
if(chaseIsOn == true){ //when there is nothing in the way anymore - continue chase if chasing
follow();
}}}}}
}}
//if no way is viable
if(Physics.Raycast(transform.position, transform.forward, hitRay, 5)){
if(hitRay.transform.tag == "TerrainCollision" || hitRay.transform.tag == "Target" || hitRay.transform.tag == "Moveable"
|| hitRay.transform.tag == "Wall"){
if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){
if(hitRay.transform.tag != null){
if(Physics.Raycast(transform.position,transform.right * -1, hitRay, 15)){
if(hitRay.transform.tag != null){
transform.Rotate(0, Time.deltaTime * 10 + 25, 0); //turn back the way you came
if(chaseIsOn == true){ //when there is nothing in the way anymore - continue chase if chasing
follow();
}}}}}}}
}
At the position where I put the //MOVE AROUND COLLISION I tried to make the AI(enemy) move the smart way around an object. If the raycast pointing to the left for example is tag == null (no collision), I wan’t the enemy to move at to the left if there is an collision forward. If the right and left raycast doesn’t return null then he should do the command to just turn transform.Rotate(0,Time.deltaTime * 10 + 25, 0); (right) which will result in a 180 degree turn going back the same way he came.
The follow() function will make the enemy chase the player.
Atm the enemy looses the target when encountering collision and doesn’t turn to the raycast that returns null.
Any help or tips would be appreciated, thanks!