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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Flyer AI for top-down game, picking random points within distances and angle

Discussion in 'Scripting' started by Stonewood1612, Apr 24, 2015.

  1. Stonewood1612

    Stonewood1612

    Joined:
    Sep 15, 2014
    Posts:
    32
    Greetings,

    I'm trying to make an AI movement script for a flying enemy in a top-down game, and I could use some help and an example on a certain part. The idea is:

    -The enemy spawns, it goes looking for a target. (Which I've already set up how it does that), the position of that target will be the 'center'.

    -It moves towards the center, once within a certain distance, it's wandering behavior starts.

    -This is the hard part: It needs to find a random point within a min and max distance from it's own transform.position, but it also needs to be within a max distance from the center. And to prevent it doing strange U-turns, it would search within a 90° angle (or another set angle) so it doesn't pick points behind it.

    -Once a point is found, start turning and moving towards it. Once close enough to that point, pick a new one.


    I hope that's clear enough to understand :p. I've looked around a bit, found a few topics that are similar to what I'm trying to do, although I still have little idea of how I'm going to handle to hard part. So I'd like to see help and an example, because there are probably many ways to do it, and not all being equally efficient or difficult.

    Note: the Y (height) of the flyer is/should be constant, so the waypoints will always have the same height as the object's height.

    Thanks
     
  2. MysterySoftware

    MysterySoftware

    Joined:
    Sep 18, 2014
    Posts:
    46
    You might wanna take a look at Unity's 'Random.insideUnitSphere' to calculate a position within a given distance.

    You could try something along those lines..
    Code (CSharp):
    1. public Vector3 origin, randomPosition;
    2. public int searched;
    3.  
    4. // The maximum distance to each point
    5. public float maxDistance = 15.0f;
    6.  
    7. // The amount of points
    8. public int searchPoint = 5;
    9.  
    10. public void Initialize () {
    11.     // Buffer the position
    12.     origin = transform.position;
    13.  
    14.     // A random position
    15.     randomPosition = (Random.insideUnitSphere * maxDistance) + origin;
    16.  
    17.     // The amount of random positions already visited
    18.     searched = 0;
    19. }
    20.  
    21. public void Do () {
    22.     if (searched < searchPoints) {
    23.             if (Vector3.Distance (transform.position, randomPosition) <= 1) {
    24.                 // A new random position
    25.                 randomPosition = (Random.insideUnitSphere * maxDistance) + origin;
    26.  
    27.                 // Increase the amount of searched positions
    28.                 searched++;
    29.             }
    30.        
    31.             // MOVE THE TARGET TO THE RANDOM POSITION
    32.             // YOUR MOVEMENT CODE HERE :D
    33.         }
    34. }