Search Unity

Avoiding obstacles

Discussion in 'Scripting' started by Slabada, Feb 16, 2022.

  1. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    Hello, I'm making a 2D game with turn-based combat, trying to make AI for enemies.
    I wrote the most primitive AI (if you can call it AI) and I can't think of how to implement it so that the enemy bypasses the obstacles that will meet his way.
    I have an idea using a beam, but I'm not sure it's a good idea.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AI : MonoBehaviour
    6. {
    7.     public Transform target;
    8.  
    9.     public Vector3 Pos;
    10.  
    11.     public bool Move = false;
    12.  
    13.     public LayerMask BlockMask;
    14.  
    15.     void Update()
    16.     {
    17.         EnemyMove();
    18.     }
    19.  
    20.     void EnemyMove()
    21.     {
    22.         if (Move == true)
    23.         {
    24.             if (target.position.x > transform.position.x)
    25.             {
    26.                 Pos = transform.position + Vector3.right * 1;
    27.                 transform.position = new Vector3(Pos.x, transform.position.y);
    28.                 if(transform.position == Pos) Move = false;
    29.             }  
    30.             if (target.position.x < transform.position.x)
    31.             {
    32.                 Pos = transform.position + Vector3.left * 1;
    33.                 transform.position = new Vector3(Pos.x, transform.position.y);
    34.                 if (transform.position == Pos) Move = false;
    35.             }
    36.  
    37.             if (target.position.y > transform.position.y)
    38.             {
    39.                 Pos = transform.position + Vector3.up * 1;
    40.                 transform.position = new Vector3(transform.position.x, Pos.y);
    41.                 if (transform.position == Pos) Move = false;
    42.             }
    43.             if (target.position.y < transform.position.y)
    44.             {
    45.                 Pos = transform.position + Vector3.down * 1;
    46.                 transform.position = new Vector3(transform.position.x, Pos.y);
    47.                 if (transform.position == Pos) Move = false;
    48.             }
    49.         }
    50.     }
    51. }
    52.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    We certainly aren't sure either. Tell us how it works out.

    As always and as I've suggested before, try some tutorials for 2D navigation. It is an extremely well-studied area of game development and nobody here could do it justice typing in this little tiny box.
     
    Slabada likes this.
  3. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    There is an idea to let the beam in the direction where the enemy will go, if the beam does not intersect with the specified layer, then it can go.
     
  4. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    All the manuals I found use Navmesh, as I understand it, this is some kind of third-party plugin, because in all videos it is installed separately, and I am limited only to the functionality that Unity itself has from the very beginning, so I would have used the wonderful NawMesh for a long time and would not have suffered :)
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    NavMesh is a built in functionality of Unity:
    https://docs.unity3d.com/Manual/nav-BuildingNavMesh.html

    Though NavMesh is also a 3d thing... not really a 2d thing. So I wonder what "manuals" you're reading that say to use NavMesh for 2d navigation.

    You also have yet to define "beam"... there is no standard programming term, unity term, or game design term that I'm aware of that is called "beam". I can presume some meanings for it, like a raycast or something, but that is an assumption and we all know what happens when we assume (ass-u-me).

    Now there is a reason that there isn't some pre-baked, one size fits all, implementation of obstacle avoidance. And that's because not ever game has the same kinds of obstacles, or the same kind of mobs that need to avoid those obstacles.

    So how about you define all of this...

    1) what are the kinds of obstacles you have?

    2) what kind of mobs do you expect to have and how you want them to behave in regards to obstacles?

    3) what have you done to try to deal with it up to this point... i.e. what is a beam?

    ...

    Also, if you're wondering what a 'mob' is... well 'mob' is a common term in the game design world (unlike beam). It's short for "mobile object". Mob's generally refer to NPCs (non-player characters) that move around. Often they are combatants, meaning you can attack them. Though not exclusive to that definition... generally speaking if someone says "mob" they usually are referring to enemy characters.

    ...

    Back at describing your problem.

    It's often good to draw a diagram of what it is you attempt to accomplish. Theorize the sorts of obstacles you'll have.

    For example... if I were to describe obstacles in Super Mario Bros. (the 1st one). The obstacles are generally vertical obstacles of some N height (where N is number of blocks) that can be leapt over. Like the green pipes or stacks of bricks. Then what we'd need to do is calculate the jump height and horizontal speed of a mob to know if it can scale an obstacle and at what position it would have to leap to scale said obstacle when moving in some direction.

    But the description of obstacle changes a lot if we were say describing a scrolling shooter like Gradius. Mobs wouldn't "leap" in a game like that. Rather they would fly around the obstacles. The obstacles could be doged on both the top and bottom rather than just the top. And there is no jump height, but rather a turning speed that would be considered in determining if the mob can make it around an obstacle in appropriate time.

    I'm not going to actually draw these and post them... but I bet you could dream up drawing an image of a general play area with a mob and what you expect it to do as it approaches a general obstacle in your game. So start there.
     
    Last edited: Feb 17, 2022
    Slabada likes this.