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

Need help for AI Wandering

Discussion in 'Scripting' started by False_Baconator, Dec 26, 2018.

  1. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    Hi, I'm trying to have my enemies and Allies wander around, but they never go down or to the left, its to the point where you cant just write it off as random chance. it seems that they don't want to go in negative directions, but I don't understand why, I could really use some help with this. here's my wandering code. Thanks in advance!

    Code (CSharp):
    1.     void Wander()
    2.     {
    3.         //Wander around, trying to find a target.
    4.  
    5.         if (PaceLength > 0)
    6.         {
    7.  
    8.  
    9.             rb.MovePosition(rb.position + MoveVelocity * Time.deltaTime);
    10.             PaceLength -= Time.deltaTime;
    11.         }
    12.         else if (Patience <= 0)
    13.         {
    14.             PaceLength = StartPaceLength;
    15.             Patience = StartPatience;
    16.  
    17.  
    18.             Decision = Rand.Next(1, 3);
    19.             if (Decision == 1)
    20.                 WanderX = 1;
    21.             else if (Decision == 2)
    22.                 WanderX = 0;
    23.             else if (Decision == 3)
    24.                 WanderX = -1;
    25.  
    26.             if (WanderX == 0)
    27.             {
    28.                 Decision = Rand.Next(1, 2);
    29.                 if (Decision == 1)
    30.                     WanderY = 1;
    31.                 else if (Decision == 2)
    32.                     WanderY = -1;
    33.             }
    34.  
    35.             if (WanderX != 0)
    36.             {
    37.                 Decision = Rand.Next(1, 3);
    38.                 if (Decision == 1)
    39.                     WanderY = 1;
    40.                 else if (Decision == 2)
    41.                     WanderY = 0;
    42.                 else if (Decision == 3)
    43.                     WanderY = -1;
    44.             }
    45.  
    46.             MoveVelocity.x = WanderX * speed;
    47.  
    48.             MoveVelocity.y = WanderY * speed;
    49.  
    50.  
    51.  
    52.         }
    53.         else
    54.         {
    55.  
    56.             Patience -= Time.deltaTime;
    57.         }
    58.  
    59.     }
     
  2. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
  3. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    matzomat likes this.
  4. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    thanks, this was really helpful!