AI - Raycast collision and follow player

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!

looks like you probably have an if error you should AND stuff together to make it easier to read and notice where it is.

this for example which is the test for only turn 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){

this is better (not best, that comes in a minute)

if(physics.raycast(transform.position,-transform.right,15) && !physics.raycast(transform.position,transform.right,15))


that checks only one however really whats best is to remove the long lines

this is best

//using hard numbers is NEVER a good idea
//get used to ALWAYS storing the number and going by that
float CastDistance = 15f;

bool LeftHit;
bool RightHit;
Raycasthit LeftHitInfo;
raycasthit RightHitinfo;

update()
{
lefthit = physics.raycast(transform.position,-transform.right,distance,out LeftHitInfo);
righthit = .....

now just

if(lefthit && !righthit)
{
//rotate left
}

You are constantly recasting the same rays over and over. Store that stuff.
Your code is hard to read because raycasts are long lines of code.

if you store and reference you’ll get rid of dozens of pointless raycasts and make the code much easier to read and probably notice what you messed up.