Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Find nearest cover? / How to store static waypoints? (2D)

Discussion in 'Navigation' started by mbiggs2334, Jun 12, 2023.

  1. mbiggs2334

    mbiggs2334

    Joined:
    Mar 31, 2022
    Posts:
    37
    Hello, hopefully this is an appropriate place for this, I’m not sure where else it would fit.

    I’m building a 2D game. I have a couple question regarding AI and ways to assign movement points.

    What’s the best practice having set places for AIs to move to? For example, how would an AI find the nearest place to take cover? Would I make dedicated gameobjects on a dedicated layer that would represent a place for an AI to take cover and then have the AI do something like a physics.CircleOverlapAll and loop through to find the nearest one? Or is there a better approach than this?

    Also what about set predefined points for say an AI patrolling an area. Would I use the game object method or would it be better to save the transform points somewhere static and read from them?

    Any help or direction to go in to learn more would be greatly appreciated, thank you.
     
  2. mbiggs2334

    mbiggs2334

    Joined:
    Mar 31, 2022
    Posts:
    37
    Anyone?
     
  3. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    Hi there,

    2 pretty big questions in one.

    AI finding cover. This is a very complex topic. What you propose there is fine.
    Make something like a boxCollider, assign it to a layer "Cover" and the use the physics overlap circle method.
    You'll then probably want to do raycasts from the player/enemy and see which coverspots are in the "open" or not. Give each one of those cover spots a cover score using paramaters like "DistanceFromEnemy", "distanceFromSelf" etc..
    Implement a method to sort the scores and pick the best cover spot and then move towards it.
    It's tricky making sure that multiple AI units don't converge to the same one though..

    Patrol is easy and there's a wonderfully clear example here on the unity docs.

    Hope this helps.
     
    mbiggs2334 likes this.