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

I'm trying to make the enemy attack me and follow me when in range

Discussion in 'Scripting' started by Dazza12, Jun 4, 2014.

  1. Dazza12

    Dazza12

    Joined:
    Jun 4, 2014
    Posts:
    4
    I have no idea what is wrong with this but it just isn't working. Also the code is in JS

    Code (csharp):
    1. var target : Transform; //the enemy's target
    2. var moveSpeed = 3; //move speed
    3. var rotationSpeed = 3; //speed of turning
    4. var attackThreshold = 3; // distance within which to attack
    5. var chaseThreshold = 10; // distance within which to start chasing
    6. var giveUpThreshold = 20; // distance beyond which AI gives up
    7. var attackRepeatTime = 1; // delay between attacks when within range
    8. var attackRange = 1.5;
    9. var TheDammage = 30;
    10.  
    11. private var chasing = false;
    12. private var attackTime = Time.time;
    13.  
    14. var myTransform : Transform; //current transform data of this enemy
    15.  
    16. function Awake()
    17. {
    18.     myTransform = transform; //cache transform data for easy access/preformance
    19. }
    20.  
    21. function Start()
    22. {
    23.      target = GameObject.FindWithTag("Player").transform; //target the player
    24. }
    25.  
    26. function Update () {
    27.  
    28.     // check distance to target every frame:
    29.     var distance = (target.position - myTransform.position).magnitude;
    30.     if (distance < attackRange)
    31.         {
    32.         attack();
    33.         }
    34.    
    35.     if (chasing) {
    36.  
    37.         //rotate to look at the player
    38.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    39.         Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    40.  
    41.         //move towards the player
    42.         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    43.  
    44.         // give up, if too far away from target:
    45.         if (distance > giveUpThreshold) {
    46.             chasing = false;
    47.         }
    48.  
    49.         // attack, if close enough, and if time is OK:
    50.         if (distance < attackThreshold && Time.time > attackTime) {
    51.             // Attack! (call whatever attack function you like here)
    52.             attackTime = Time.time + attackRepeatTime;
    53.         }
    54.  
    55.     } else {
    56.         // not currently chasing.
    57.  
    58.         // start chasing if target comes close enough
    59.         if (distance < chaseThreshold) {
    60.             chasing = true;
    61.         }
    62.     }
    63. function Attack();
    64. {
    65.     if (Time.time > attackTime)
    66.     {
    67.         Target.SendMessage("ApplyDammage", TheDammage);
    68.         Debug.Log("The Enemy Has Attacked");
    69.         attackTime = Time.time + attackRepeatTime;
    70.     }
    71. }
    72.  
    73. function ApplyDammage ();
    74. {
    75.     chaseRange += 30;
    76.     moveSpeed += 2;
    77.     lookAtDistance +=40;
    78. }
    79.  
    80. }
     
    Last edited: Jun 4, 2014
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    not at all? is the script attached to anything?
     
  4. rrrrhoi

    rrrrhoi

    Joined:
    Jun 4, 2014
    Posts:
    9
    if (distance < attackThreshold && Time.time > attackTime)
     
  5. Dazza12

    Dazza12

    Joined:
    Jun 4, 2014
    Posts:
    4
    its attached to my enemy character and a message comes up saying
    Assets/attackwheninrange.js(59,10): BCE0044: expecting (, found 'Attack'.
    Assets/attackwheninrange.js(69,10): BCE0044: expecting (, found 'ApplyDammage'.
     
  6. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    function Attack(); <===== semicolon

    function ApplyDammage (); <===== semicolon

    You declared your Attack and ApplyDammage function with semicolon at the end of the line...
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ah, ok, so we're talking syntax errors... like have

    Code (csharp):
    1.  
    2. Attack();
    3. {...}
    4.  
    that ';' is a code equivalent of a fullstop, you don't want to end there

    and scrolling up we see

    Code (csharp):
    1.  
    2.  
    3. if (distance < attackThreshold && Time.time > attackTime) {
    4. ...
    5. ...
    6. }
    7.  
    8. } else {
    9. ...
    10.  
    open {, close }, close } (huh??)
     
  8. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Actually he was closing the if (chasing) from about 15 lines before but he doesn't close the function if I counted right...
     
  9. rrrrhoi

    rrrrhoi

    Joined:
    Jun 4, 2014
    Posts:
    9
    and...



    [Mistake]
    functions "Attack" and "ApplyDamage" are declared in "Update".



    [Mistake]
    private var attackTime = Time.time; //maybe you cannot use unity-classes at here, move it to "Awake"

    [Change]
    private var attackTime = 0;
    ...
    function Awake()
    {
    attackTime = Time.time;
    ...


    the player has "ApplyDamage" function? if not..
    [Mistake]
    target.SendMessage("ApplyDammage", TheDammage);

    [Change]
    ApplyDammage( TheDammage ); // you should modify "ApplyDammage" function to can accept "TheDamage"




    and some mistypes..
     
  10. Dazza12

    Dazza12

    Joined:
    Jun 4, 2014
    Posts:
    4
    I managed to find the problem, sorry, this was my first time posting on the forums, but this is the new script, (a reminder that the script is .js) and a new problem has come up

    InvalidCastException: Cannot cast from source type to destination type.
    (wrapper dynamic-method) UnityEngine.Transform.Transform$get_position$ (object,object[]) <IL 0x00001, 0x0002f>
    Boo.Lang.Runtime.RuntimeServices.GetProperty (object,string) <IL 0x00043, 0x00126>
    UnityScript.Lang.UnityRuntimeServices.GetProperty (object,string) <IL 0x00017, 0x000ac>
    AdvancedAI.Update () (at Assets/AdvancedAI.js:25)

    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 Damping = 6.0;
    8. var attackRepeatTime = 1;
    9.  
    10. var TheDamage = 40;
    11.  
    12. private var attackTime : float;
    13.  
    14. var controller : CharacterController;
    15. var gravity : float = 20.0;
    16. private var MoveDirection : Vector3 = Vector3.zero;
    17.  
    18. function Start ()
    19. {
    20.     attackTime = Time.time;
    21. }
    22.  
    23. function Update ()
    24. {
    25.     Distance = Vector3.Distance(Target.position, transform.position);
    26.  
    27.  
    28.     if (Distance < attackRange)
    29.     {
    30.         attack();
    31.     }
    32.     else if (Distance < chaseRange)
    33.     {
    34.         chase ();
    35.     }
    36. }
    37.  
    38.  
    39. function chase ()
    40. {
    41.  
    42.  
    43.     moveDirection = transform.forward;
    44.     moveDirection *= moveSpeed;  
    45.     moveDirection.y -= gravity * Time.deltaTime;
    46.     controller.Move(moveDirection * Time.deltaTime);
    47. }
    48.  
    49. function attack ()
    50. {
    51.     if (Time.time > attackTime)
    52.     {
    53.         Target.SendMessage("ApplyDamage", TheDamage);
    54.         Debug.Log("The Enemy Has Attacked");
    55.         attackTime = Time.time + attackRepeatTime;
    56.     }
    57. }
    58.  
    59. function ApplyDamage ()
    60. {
    61.     chaseRange += 30;
    62.     moveSpeed += 2;
    63.     lookAtDistance += 40;
    64. }
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ah, so it is.... <mutters about code tags> lol

    you might also want to look at your logic

    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.  
    5.     // check distance to target every frame:
    6.     var distance = (target.position - myTransform.position).magnitude;
    7.     if (distance < attackRange)
    8.     {
    9.         attack();
    10.     }
    11.  
    12.     if (chasing)
    13.     {
    14.  
    15.         ...
    16.  
    17.         // attack, if close enough, and if time is OK:
    18.         if (distance < attackThreshold && Time.time > attackTime)
    19.         {
    20.         // Attack! (call whatever attack function you like here)
    21.         attackTime = Time.time + attackRepeatTime;
    22.         }
    23.      
    24.         ...
    25.  
    26. }      
    27.      
    28. function Attack();
    29. {
    30.     if (Time.time > attackTime)
    31.     {
    32.         Target.SendMessage("ApplyDammage", TheDammage);
    33.         Debug.Log("The Enemy Has Attacked");
    34.         attackTime = Time.time + attackRepeatTime;
    35.     }
    36. }
    37.  
    this sequence looks wrong... if you're close attack (which updates the attack time) if you are chasing and the attack time runs out, update the attack time. I'm assuming the attack range is always less than the chasing range, so i think
    this bit
    Code (csharp):
    1.  
    2.     if (distance < attackRange)
    3.     {
    4.         attack();
    5.     }
    6.  
    should be in here
    Code (csharp):
    1.  
    2. {
    3.         // Attack! (call whatever attack function you like here)
    4. Attack(); // <<<<<<<< here
    5.        attackTime = Time.time + attackRepeatTime;
    6.         }
    7.  
    also in the code posted above that call to attack() is lower case a, not uppercase A as the function is called. It's case sensitive.
     
    Last edited: Jun 4, 2014
  12. rrrrhoi

    rrrrhoi

    Joined:
    Jun 4, 2014
    Posts:
    9
  13. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148

    Code (csharp):
    1. var Target = Transform;
    should be
    Code (csharp):
    1.  
    2. var Target : Transform;
    3.  
    you'll need to assign the target somewhere, or populate it in the inspector
     
  14. rrrrhoi

    rrrrhoi

    Joined:
    Jun 4, 2014
    Posts:
    9
  15. Dazza12

    Dazza12

    Joined:
    Jun 4, 2014
    Posts:
    4
    Yeah I will, thanks for your time

    Its working now so :p

    Thanks again