Search Unity

How to make enemy spread out only if further from player than the one in front.

Discussion in 'Scripting' started by Masterredlime, Nov 23, 2017.

  1. Masterredlime

    Masterredlime

    Joined:
    Sep 11, 2017
    Posts:
    12
    So I've included a script in my robots to make them spread out to prevent them from cluttering using a for each statement, however I wanted it to only spread out if its the one further back from the player than the other robot who is closer to the player and in range.

    Here is the statement:
    Code (CSharp):
    1. public void AvoidEnemies()
    2.     {
    3.         foreach (GameObject enemy in EnemiesNearby)
    4.         {
    5.             // Find distance between robot and other enemies    
    6.             float enemyDistance = Vector3.Distance(transform.position, enemy.transform.position);
    7.             // Find distance between other robots and player
    8.             float robotsDistanceToPlayer = Vector3.Distance(enemy.transform.position, target.transform.position);
    9.  
    10.             if (enemyDistance < minEnemyDistance)
    11.             {        
    12.                 if (retreating == false) // Don't spread out yet if retreating
    13.                 {
    14.                     if (state == RobotState.STANDBY || state == RobotState.STANDBY_LOW)
    15.                     {
    16.                         if (targetDistance > robotsDistanceToPlayer) // Only spread out if the one further back
    17.                         {
    18.                             transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x + (transform.position.x - enemy.transform.position.x), transform.position.y, transform.position.z + (transform.position.z - enemy.transform.position.z)), movementSpeed * Time.deltaTime);
    19.                             // Include some way of making enemies move away from each other
    20.                         }
    21.                     }
    22.                 }
    23.             }
    I tried adding the statement that if the target distance (robot's distance from the player) is greater than the other robots distance from the player, only then will the robots spread out. However this statement doesn't seem to work as the robots nearer to the player still seem to wanna spread out and be "pushed" forward by the robots behind.
    Is there a way that I can get the robot to only compare its distance with the ones within spreading out range and then only get the one who is further from the player to spread out?
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I believe what you are trying to do is more complex than you will need... since in your example each robot would need to know what robots are near it and then have each check their location in relation to the player and then to each other in order to get an idea of who moves where. It may be easier and more effective to have each robot check to see if anything blocks its view of the player and if so move over. Better yet, have a forward check and a check forward +/- 45 degrees (left/right) to help decide which way it might want to slide over. Raycast or one of the other physics cast variants should get the job done for the line of sight checks