Dodging an Obstacle without Navmeshagent

Asked

Viewed 389 times

4

I need to create an obstacle clearance script but I can’t use Navmeshagent because this project depends on implementations where the Navmeshagent doesn’t work.

See that in the image below there are two cubes and an 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 direction of the blue cube using a Vector3.forward and LookAt, but if there is any obstacle in front of the red cube the locomotion must be modified so that there is a deviation.

The problem is that I’m thinking if the best method to avoid the obstacle would be to take the size of the object in front 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 its locomotion to the cube blue. Or shoot a rotating Raycast that will take the shortest free path and add up the position. But how to do this?

What would be the right way to make this detour? remembering that I cannot use the Navmeshagent in no case.

inserir a descrição da imagem aqui

  • 1

    Pathfinding without navmesh in a free environment is complicated... You may not even use the native Unity, but Navmesh is very good for this type of situation. What you quoted is a "donkey" method, it would work well with an obstacle, but it would be horrific if the environment were more complex, and it certainly could get stuck in a "U" if it found a dead end.

  • The problem is that Unity’s Navmesh only works with terrains, and I need to create this script for navigating on a sphere.

  • 1

    Ah, if you’re going to pathfinding a planet or something like that then your best way out is an A* star itself or a derivative of it. http://gamedev.stackexchange.com/questions/53866/pathfinding-on-a-uneven-planetary-surface

  • 1

    Well, first of all if your problem is with a sphere, make that clear in the question. If possible, also provide an example project so that someone can help you without having the trouble to create everything from scratch. The link that the colleague @DH. mentions is the best question related to what you need. There is this other on the Soen, whose only answer provides some interesting details (tutorial for you to create an A* in Unity3d, and even a link to Asset Store with a project that already does what you need).

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:

http://docs.unity3d.com/Manual/nav-BuildingNavMesh.html (navigation Static)

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

https://www.youtube.com/watch?v=45_EABIc7VA

Now, if none of the above solutions help you, what I would suggest to solve your deviation problem would be to put some objects (even cubes) without box Ollider and without mesh Nderer. Then by script you take them and check if the player is further away than one of these access points or if the raycast detects an obstacle, walk to the detour, 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 helped, good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.