Search Unity

how to make AI walk aimlessly?

Discussion in 'Scripting' started by luhjgh, Jul 13, 2011.

  1. luhjgh

    luhjgh

    Joined:
    Jan 1, 2011
    Posts:
    128
    I am trying to make some AI walk aimlessly around the city, but I just don't know how...

    I thought of adding a sphere in front of the AI, disabling the spheres mesh renderer, making the sphere a child of the AI, then using the enemy_follow script (graciously provided by Afisicos!) to make the AI follow the Sphere... But then they would only walk in a straight line...


    Here is the enemy_follow code if you need it:

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var vel_vec : Vector3;
    4. var speed : float;
    5. var distance_byte : float;
    6. var player_life : Player_life_script;
    7.  
    8. function Update()
    9. {
    10. vel_vec = target.position - transform.position; // creates the vector from the enemy to you.
    11. transform.LookAt(target); // enemy looks to you
    12. transform.Translate(Vector3.forward * speed * Time.deltaTime); // enemy walks to you
    13. if (vel_vec.magnitude <= distance_byte) //if is near
    14. {
    15. animation.Play("byte", PlayMode.StopAll);
    16. vel_vec = Vector3.zero;
    17. }
    18. else if (vel_vec.magnitude > distance_byte + 5) // if is far
    19. animation.Play("walk", PlayMode.StopAll);
    20. }
    does anyone know how to make them just walk aimlessly?
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,180
    you can use random.range to get random values in the range of your level size for x + z (y is determined by the height at this position).
    but i'm not sure why you want that. the i in ai means intelligence so why should an npc walk without an aim. to make a game imersive the npcs should do usefull and meaningfull things like collecting resources or food. if you want this declare some positions for them to do this before hand and choose one of these positions where they can walk to. if you want them to patrol an area you should also predefine some patrol paths. keep in mind that not all your terrain may be walkable (rocks, valleys etc) and to avoid using pathfinding (for lots of npcs) they should follow a path of waypoints close to each other.
    if thats not what you are looking for please explain your intentions a bit more.
     
  3. Afisicos

    Afisicos

    Joined:
    Nov 13, 2010
    Posts:
    326
    Hey, thanks for the credits xD.

    You can use a sphere as u say, but the position of the sphere can be random as say exigous.

    I think that you have to do an script to fix a randomly position for the sphere, and then aply a moving (u can use enemy_follow modified) to follow the sphere and go to it.

    Then, when the enemy touch the sphere, recalculating a new position of the sphere.



    This starts to be an IA when the enemy is in front of a building, and he know the path to follow.

    I can script it but i need to be relaxing and out of my work xD.
     
  4. TiG

    TiG

    Joined:
    Feb 28, 2011
    Posts:
    308
    The AI can walk for a random but not too short amount of time in a direction, at the end of the time he can turn a random amount of angle to a side and repeat this. To avoid bumping it can use raycasts.