Search Unity

Pathfinding

Discussion in 'Scripting' started by Sh00pus, Mar 31, 2010.

  1. Sh00pus

    Sh00pus

    Joined:
    Mar 5, 2010
    Posts:
    44
    Does anyone know any tutorials out there that could teach me about pathfinding? I need this for my enemies in my game because I find the quite "stupid" at trying to find people.
     
  2. fyrlandt

    fyrlandt

    Joined:
    Aug 20, 2009
    Posts:
    80
    there's a thread here about A* pathfinding :D
    that might help you :D
     
  3. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    So basically you want your enemies to patrol around waypoints, like around some buildings or something. or are you looking for the enemies to follow the player and then when chasing him to be able to avoid objects, or are you looking for the enemies to map out the shortest way to intercept the player? I know how to do waypoints and going around objects while chasing the player, but the other one I have no idea how to accomplish. If you want I can post a simple script for waypoints and dodging around objects while chasing the player.
     
  4. Sh00pus

    Sh00pus

    Joined:
    Mar 5, 2010
    Posts:
    44
    I need to have the enemy chase the player while able to avoid the objects.
     
  5. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    I have just the script for you. I'll post it in a sec.

    EDIT: Ok here is some AI script I've been working on, just change the left/right and forward variables to however far you want the raycasts to go(The smaller the number, the shorter the raycasts. the shorter the raycasts are the more manuverable the enemy will be in tight spaces, but he will also go much closer to any obstacle). Also you can edit the chaseRange for how far the enemy will start chasing you from, and minDistance is how close the enemy will come to you before he stops, this I use to initiate the enemies melee attack if the enemy comes close to my player.

    Code (csharp):
    1. var rayCasting : boolean;
    2. var chaseRange : float;
    3. var minDistance : float;
    4. var speed : float;
    5. var leftRight : float;
    6. var forward : float;
    7. var follower : Transform;
    8. private var range : float;
    9.  
    10. function Update(){
    11. var right = transform.TransformDirection(Vector3.right);
    12. var left = transform.TransformDirection(Vector3.left);
    13. var fwd = transform.TransformDirection(Vector3.forward);
    14. range = Vector3.Distance(follower.position,transform.position);
    15. var point : Vector3 = transform.position;
    16. var myPoint : Vector3 = transform.position;
    17.  
    18. if(Vector3.Distance(follower.position, transform.position) < minDistance){
    19. follower.Translate(Vector3.zero);
    20. transform.LookAt(new Vector3(point.x, myPoint.y, point.z));
    21. rayCasting = false;
    22. }
    23. else if(Vector3.Distance(follower.position, transform.position) > minDistance){
    24. rayCasting = true;
    25. }
    26.  
    27. if (rayCasting == true){
    28. if (range <= chaseRange ){
    29. if (!Physics.Raycast(transform.position, fwd, forward)  !Physics.Raycast(transform.position, right, leftRight)  !Physics.Raycast(transform.position, left, leftRight)){
    30. transform.LookAt(new Vector3(point.x, myPoint.y, point.z));
    31. transform.Translate(Vector3.forward * speed * Time.deltaTime);
    32. }
    33. else if (!Physics.Raycast(transform.position, fwd, forward)) {
    34. transform.Translate(Vector3.forward * speed * Time.deltaTime);
    35. }
    36. else if (!Physics.Raycast(transform.position, right, leftRight)) {
    37. transform.Translate(Vector3.zero);
    38. transform.Rotate(0,30,0);
    39. }
    40. else if (!Physics.Raycast(transform.position, left, leftRight)) {
    41. transform.Translate(Vector3.zero);
    42. transform.Rotate(0,-30,0);
    43. }
    44. }
    45. }
    46. }
     
  6. Sh00pus

    Sh00pus

    Joined:
    Mar 5, 2010
    Posts:
    44
    Thanks! should I check the little box that says raycast?

    Edit: I checked the raycast and the enemy started moving. But when i walked close to the enemy he didn't follow me. he wanders around an avoids walls really well. I set the following distance to 30.
     
  7. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    @ Sh00pus:

    I just took a little look through the above code and..

    - no that checkbox should automatically come on when the distance between the enemy and the follower is greater than minDistance.

    - I think that the code above should actually say:

    Code (csharp):
    1. var point : Vector3 = follower.position;
    2. var myPoint : Vector3 = transform.position;
    ... because I doubt that 'point' and myPoint are supposed to be the same thing as has been written above?
     
  8. Sh00pus

    Sh00pus

    Joined:
    Mar 5, 2010
    Posts:
    44
    Thanks! It works Now
     
  9. Jfhutchi

    Jfhutchi

    Joined:
    Jan 5, 2010
    Posts:
    32
    Do you know what I'd have to change in this script to get it to follow a item of type GameObject?