Search Unity

A*Pathfinding repath?

Discussion in 'Scripting' started by Testnia, Aug 2, 2014.

  1. Testnia

    Testnia

    Joined:
    Jul 4, 2014
    Posts:
    11
    Hi, I've being trying to get my script to repath an object such that it tracks a user controlled character.

    This is the current script I'm working on.


    Code (JavaScript):
    1. import Pathfinding;
    2.  
    3. //var tankTurret : Transform;
    4. var tankBody : Transform;
    5. //var tankCompass: Transform;
    6. var turnSpeed : float = 10.0;
    7.  
    8. var targetPosition : Vector3;
    9. var seeker : Seeker;
    10. var controller : CharacterController;
    11. var path : Path;
    12. var speed : float = 100;
    13. var nextWaypointDistance : float = 3.0;
    14. var repathRate : float = 0.1;
    15. private var currentWaypoint : int = 0;
    16.  
    17. function Start()
    18. {
    19.     targetPosition = GameObject.FindWithTag("GTO").transform.position;
    20.     GetNewPath();
    21. }
    22.  
    23. function GetNewPath()
    24. {
    25.     Debug.Log("GETTING NEW PATH!");
    26.     seeker.StartPath(transform.position,targetPosition,OnPathComplete);
    27.    
    28.        
    29.     /*if (seeker.IsDone())
    30.     {
    31.     seeker.StartPath(transform.position, targetPosition, OnPathComplete);
    32.     }*/
    33. }
    34.  
    35. function OnPathComplete(newPath : Path)
    36. {
    37.     if(!GetNewPath.error)
    38.     {
    39.         path = newPath;
    40.         currentWaypoint = 0;
    41.     }
    42. }
    43. function FixedUpdate()
    44. {        
    45.     if(path==null)
    46.     {
    47.         return;
    48. //            Debug.Log("NO PATH!?");
    49.     }
    50.     /*if(currentWaypoint >= path.vectorPath.Length)
    51.     {
    52.         return;
    53.     }*/
    54.    
    55.     var dir : Vector3 = (path.vectorPath[currentWaypoint]-transform.position).normalized;
    56.    
    57.     controller.SimpleMove(dir);
    58.    
    59.     //tankCompass.LookAt(path.vectorPath[currentWaypoint]);
    60.     //tankBody.rotation = Quaternion.Lerp(tankBody.rotation, tankBody.rotation, Time.deltaTime*turnSpeed);
    61.    
    62.     if(Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance)
    63.     {
    64.         currentWaypoint++;
    65.     }
    66.    
    67. }
    68.  
    Would appreciate any input on what I could add / modify to get it working.