Deviating from an obstacle without NavMeshAgent

4

I need to create an obstacle bypass script but I can not use NavMeshAgent to do this because this project relies on implementations where NavMeshAgent does not work.

Notice that in the image below there are two cubes and one obstacle, the blue cube represents the player while the red cube represents the enemy. The red cube has a Raycast that identifies whether the blue cube is near (red line), and then moves in the blue cube direction using a Vector3.forward and LookAt , but if there is any obstacle ahead of the red cube the locomotion must be modified so that there is a deviation.

The problem is that I'm wondering if the best method to deviate from the obstacle would be to take the size of the object ahead and calculate the distance from its center to its edge + the size of the red cube to direct the red cube there and then continue your journey to the blue cube. Or shoot a rotating Raycast that will take the shortest free path and add up the position. But how do you do that?

What would be the correct way to do this deviation? remembering that I can not use NavMeshAgent in any case.

    
asked by anonymous 07.04.2016 / 19:14

1 answer

2

If you're just avoiding navmesh because of the terrain, know that it works on any object as long as it's marked "static." You can see this in the documentation:

link (navigation static)

Or, you can see in this video where navmesh is used in a cube:

link

Now, if none of the solutions above help you, what I would suggest to solve your deviation problem would be to put some objects (even cubes) without box collider and without mesh renderer. Then by script you get them and check if the player is further away than one of these access points or if raycast detects an obstacle, walk to the deviation, then to the player.

public GameObject[] desvios;
private Bool obstaculo = false; // Coloca true quando o raycast pegar
public GameObject player, alvo; //Pra onde o inimigo vai


void Start(){
    desvios = GameObject.FindGameObjectsWithTag("Desvios");
}
void Update(){
    if(obstaculo){
      Vector3 playerPosition = player.transform.position;
      Float distPlayer = Vector3.distance(playerPosition, this.transform.position); // distancia entre inimigo e player
      for(int i=0; i<desvios.lenght; i++){
         Float distDesvio = Vector3.distance(this.transform.position, desvios[i].trasform.position);
         // Checa se a distancia até o player é maior que a do obstaculo, se move primeiro pro obstaculo (alvo)
         if(playerPosition > distDesvio){
            alvo = desvios[i];
         }
      }
    }
}

I hope I have helped, good luck!

    
12.04.2016 / 16:16