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. Dismiss Notice

Enemy Ai with waypoints

Discussion in 'Scripting' started by phedon, Oct 26, 2014.

  1. phedon

    phedon

    Joined:
    Dec 7, 2013
    Posts:
    10
    I made this script,it is an enemy ai with the waypoints inside the enemy logik script,before i merge the two scripts,they were working except that they were interfere each other,the enemy started going towards the waypoints,but when the player was going near him where some strange results,so i tied to put them in one script,but now the enemy starts looking the first waypoint but he doesnt move toward it,When the player comes near the script goes on as it is made to go.I dont have internet right now so it is somewhat difficult to search,for an answerIf you can give me some hint it will be useful.Thank you.

    var Distance;
    var Target : Transform;
    var lookAtDistance = 25.0;
    var chaseRange = 15.0;
    var attackRange = 1.5;
    var moveSpeed = 5.0;
    //var runSpeed = 10.0;
    var Damping = 6.0;
    var attackRepeatTime = 1;
    var TheAnimator : Animator;
    var TheDamage = 40;

    private var attackTime : float;

    var controller : CharacterController;
    var gravity : float = 20.0;
    private var MoveDirection : Vector3 = Vector3.zero;



    var waypoint : Transform[];
    private var currentWaypoint : int;




    function Start ()
    {
    attackTime = Time.time;
    }

    function Update ()
    {
    Distance = Vector3.Distance(Target.position, transform.position);

    if (Distance > lookAtDistance)
    {
    Patrol ();
    }


    if (Distance < lookAtDistance)
    {
    lookAt();
    }

    if (Distance < attackRange)
    {
    attack();
    }
    else if (Distance < chaseRange)
    {
    chase ();
    }
    }



    function Patrol()
    {
    if(currentWaypoint <= waypoint.Length)
    {
    var WaypointTarget : Vector3 = waypoint[currentWaypoint].position;
    var moveDirectionWp : Vector3 = WaypointTarget - transform.position;
    var velocity = rigidbody.velocity;

    transform.LookAt(waypoint[currentWaypoint]);



    if(moveDirectionWp.magnitude <= 1)
    {
    currentWaypoint++;

    }
    else
    {
    velocity = moveDirectionWp.normalized*moveSpeed;

    }
    }

    if ( currentWaypoint >= waypoint.Length )
    {
    currentWaypoint = 0;
    }

    rigidbody.velocity = velocity;
    }

    function lookAt ()
    {
    //renderer.material.color = Color.yellow;
    var rotation = Quaternion.LookRotation(Target.position - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
    }

    function chase ()
    {
    //renderer.material.color = Color.red;

    moveDirection = transform.forward;
    moveDirection *= moveSpeed;

    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
    TheAnimator.SetBool("Run", true);
    TheAnimator.SetBool("Combat", false);
    }

    function attack ()
    {
    if (Time.time > attackTime)
    {
    Target.SendMessage("ApplyDamage", TheDamage);
    Debug.Log("The Enemy Has Attacked");
    attackTime = Time.time + attackRepeatTime;
    TheAnimator.SetBool("Combat", true);
    TheAnimator.SetBool("Run", false);
    }
    }

    function ApplyDamage ()
    {
    chaseRange += 30;
    moveSpeed += 2;
    lookAtDistance += 40;
    }
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
  3. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    D: It's okay *pats head*
    It'll be alright, shush my child :3
    here i did it for you
    Code (CSharp):
    1. var Distance;
    2. var Target : Transform;
    3. var lookAtDistance = 25.0;
    4. var chaseRange = 15.0;
    5. var attackRange = 1.5;
    6. var moveSpeed = 5.0;
    7. //var runSpeed = 10.0;
    8. var Damping = 6.0;
    9. var attackRepeatTime = 1;
    10. var TheAnimator : Animator;
    11. var TheDamage = 40;
    12.  
    13. private var attackTime : float;
    14.  
    15. var controller : CharacterController;
    16. var gravity : float = 20.0;
    17. private var MoveDirection : Vector3 = Vector3.zero;
    18.  
    19.  
    20.  
    21. var waypoint : Transform[];
    22. private var currentWaypoint : int;
    23.  
    24.  
    25.  
    26.  
    27. function Start ()
    28. {
    29. attackTime = Time.time;
    30. }
    31.  
    32. function Update ()
    33. {
    34. Distance = Vector3.Distance(Target.position, transform.position);
    35.  
    36. if (Distance > lookAtDistance)
    37. {
    38. Patrol ();
    39. }
    40.  
    41.  
    42. if (Distance < lookAtDistance)
    43. {
    44. lookAt();
    45. }
    46.  
    47. if (Distance < attackRange)
    48. {
    49. attack();
    50. }
    51. else if (Distance < chaseRange)
    52. {
    53. chase ();
    54. }
    55. }
    56.  
    57.  
    58.  
    59. function Patrol()
    60. {
    61. if(currentWaypoint <= waypoint.Length)
    62. {
    63. var WaypointTarget : Vector3 = waypoint[currentWaypoint].position;
    64. var moveDirectionWp : Vector3 = WaypointTarget - transform.position;
    65. var velocity = rigidbody.velocity;
    66.  
    67. transform.LookAt(waypoint[currentWaypoint]);
    68.  
    69.  
    70.  
    71. if(moveDirectionWp.magnitude <= 1)
    72. {
    73. currentWaypoint++;
    74.  
    75. }
    76. else
    77. {
    78. velocity = moveDirectionWp.normalized*moveSpeed;
    79.  
    80. }
    81. }
    82.  
    83. if ( currentWaypoint >= waypoint.Length )
    84. {
    85. currentWaypoint = 0;
    86. }
    87.  
    88. rigidbody.velocity = velocity;
    89. }
    90.  
    91. function lookAt ()
    92. {
    93. //renderer.material.color = Color.yellow;
    94. var rotation = Quaternion.LookRotation(Target.position - transform.position);
    95. transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
    96. }
    97.  
    98. function chase ()
    99. {
    100. //renderer.material.color = Color.red;
    101.  
    102. moveDirection = transform.forward;
    103. moveDirection *= moveSpeed;
    104.  
    105. moveDirection.y -= gravity * Time.deltaTime;
    106. controller.Move(moveDirection * Time.deltaTime);
    107. TheAnimator.SetBool("Run", true);
    108. TheAnimator.SetBool("Combat", false);
    109. }
    110.  
    111. function attack ()
    112. {
    113. if (Time.time > attackTime)
    114. {
    115. Target.SendMessage("ApplyDamage", TheDamage);
    116. Debug.Log("The Enemy Has Attacked");
    117. attackTime = Time.time + attackRepeatTime;
    118. TheAnimator.SetBool("Combat", true);
    119. TheAnimator.SetBool("Run", false);
    120. }
    121. }
    122.  
    123. function ApplyDamage ()
    124. {
    125. chaseRange += 30;
    126. moveSpeed += 2;
    127. lookAtDistance += 40;
    128. }
    F***, that indentation though
     
  5. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    But what is he asking... where is the relevant code?

    Shouldn't we at least get a hint of what the intentions are?

    Why not "I have this issue... I have this question" and not "this doesn't work, read my mind and make it work"
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    (╯°□°)╯︵ ┻━┻
    You're a wizard u can do dis!
    I believe in u!!!
     
  7. phedon

    phedon

    Joined:
    Dec 7, 2013
    Posts:
    10
    Thank you,sorry for the code tags,but i was in hurry,i think i said that the enemy doesnt goes towards the waypoints,he looks at them but he doesnt move.After the player comes near him,he chases the player.The move towards the waypoints doesnt work,all the others script works,Thank you