Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Wandering Ai

Discussion in 'Navigation' started by unity_Nf9uFA8NFOkyMQ, Mar 8, 2019.

  1. unity_Nf9uFA8NFOkyMQ

    unity_Nf9uFA8NFOkyMQ

    Joined:
    Feb 9, 2019
    Posts:
    5
    Hi everybody!

    I need a little bit of help. I'm working on an augmented reality app and i need to make the chikens wander around randomly. I found this tutorial and whit the help of it i wrote this code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class wonderai : MonoBehaviour {
    6.     public float movesSpeed = 3f;
    7.     public float rotSpeed = 100f;
    8.  
    9.     private bool isWandering = false;
    10.     private bool isRotatingLeft = false;
    11.     private bool isRotatingRight = false;
    12.     private bool isWalking = false;
    13.  
    14.  
    15.  
    16.  
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.      
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (isWandering == false) ;
    28.         {
    29.             StartCoroutine(Wander());
    30.          
    31.         }
    32.         if(isRotatingRight == true) ;
    33.         {
    34.             transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
    35.         }
    36.         if (isRotatingLeft == true) ;
    37.         {
    38.             transform.Rotate(transform.up * Time.deltaTime * -rotSpeed);
    39.         }
    40.         if (isWalking == true) ;
    41.         transform.position += transform.forward * movesSpeed * Time.deltaTime;
    42.  
    43.  
    44.  
    45.     }
    46.     IEnumerator Wander()
    47.     {
    48.         int rotTime = Random.Range(1, 3);
    49.         int rotateWaite = Random.Range(1, 4);
    50.         int rotateLorR = Random.Range(1, 2);
    51.         int walkWait = Random.Range(1, 4);
    52.         int walkTime = Random.Range(1, 5);
    53.  
    54.  
    55.  
    56.         isWandering = true;
    57.  
    58.         yield return new WaitForSeconds(walkWait);
    59.        isWalking = true;
    60.         yield return new WaitForSeconds(walkTime);
    61.         isWalking = false;
    62.         yield return new WaitForSeconds(rotateWaite);
    63.         if (rotateLorR == 1)
    64.         {
    65.             isRotatingRight = true;
    66.             yield return new WaitForSeconds(rotTime);
    67.             isRotatingRight = false;
    68.  
    69.         }
    70.         if (rotateLorR == 2)
    71.         {
    72.             isRotatingLeft = true;
    73.             yield return new WaitForSeconds(rotTime);
    74.             isRotatingLeft = false;
    75.  
    76.         }
    77.         isWandering = false;
    78.  
    79.  
    80.  
    81.     }
    82. }
    83.  
    84.  
    The problame is that when i start the game i can't see the chiken but i can see its shadow so the chicken is floating somewhere. If i add a rigid body component, the chiken will just fall down.

    The other problem is that the chiken is not wandering around its just going in a straight line. It must be something with the code.(I'm a beginer, and i know its a stupid thing to start with this but i is need this for a project).

    I've included the project file itself as a unity package also the target object png. I've also made a non AR version so you dont have to print anything.

    Download here
    I'm using Unity 2018.3.5f1 (64-bit)
    Thank you!
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    431
    Okay, select the chickens when you start playing in the hierarchy window. That way you should be able to see where exactly they are.
    As for your wandering code going in a straight line, that's the same as what the tutorial shows, no? The object rotates a little, stops, walks forwards a bit, stops again, and rotates again.
     
  3. JohnSmillie

    JohnSmillie

    Joined:
    Mar 20, 2019
    Posts:
    2
    I know exactly whats wrong, tested out your code and it works for me after fixing the problem. Here's the mistake:

    Code (CSharp):
    1. if (isWandering == false) ;
    2.         {
    3.             StartCoroutine(Wander());
    4.  
    5.         }
    6.         if (isRotatingRight == true) ;
    7.         {
    8.             transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
    9.         }
    10.         if (isRotatingLeft == true) ;
    11.         {
    12.             transform.Rotate(transform.up * Time.deltaTime * -rotSpeed);
    13.         }
    14.         if (isWalking == true) ;
    15.         transform.position += transform.forward * movesSpeed * Time.deltaTime;
    Just a simple rookie mistake! Never put semi colons after if statements or it renders them useless. Just remove those and it should work! :)
     
unityunity