Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Spaceship AI Recommendations?

Discussion in 'Scripting' started by Spudly1701, Nov 2, 2021.

  1. Spudly1701

    Spudly1701

    Joined:
    Aug 25, 2013
    Posts:
    36
    Hi all,

    Would anyone have any recommendations for a Spaceship AI asset or a tutorial on creating something?

    Basically just needs to patrol an area (ideally without waypoints, and maybe avoid obstacles) and chase the player (attempting to collide with it) once a condition is met.

    I've looked around the asset store etc, but the ones available seem to be overkill for what I need.

    Thanks
    Spud
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Been thinking about that myself and the methods I've seen make use of raycasts which detect the player and obstacles to navigate around or towards points. Another one I was thinking of would be to randomly instantiate empties with TriggerColliders within a certain radius to prevent any weirdness happening.

    So what I would do is have the empty spawn in at a random point as a patrol path or have pre-designated ones and then you use raycasting for the 'pathfinding' for the ships. One thing I've noticed that space sims tend to do is they use space stations and jumpgates as end points or waypoints and that creates a more convincing traffic pattern rather than just have them wondering randomly. Another thing I would do is when I spawn the empties with the trigger colliders I would have them check that they're not within any other gameobjects or obstacles so they always spawn in empty space.

    Have maybe as well some kind of global list of waypoints to keep track of everything and when the NPCs hit a waypoint that triggers a boolean and whatnot then they move onto the next one in the list.
     
  3. ubbelito

    ubbelito

    Joined:
    Sep 24, 2018
    Posts:
    23
    Not an expert, but you may want to check out steering behavior. While not being (I think) a perfect fit for a space game, it could give some insights and ideas.

    https://gamedevelopment.tutsplus.com/series/understanding-steering-behaviors--gamedev-12732

    For obstacle avoidance, I've seen projects where each actor uses multiple rays (mostly toward the movement direction) and if the ray collides, with an object, steering is altered to avoid that obstacle.

    For instance, each actor could fire off 3x3 rays in a "grid" in front of it, and steering away from any rays that hit a solid object. This should lead to avoiding those obstacles.