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

Problem with enemy that should attack

Discussion in 'Scripting' started by Kev1807, May 4, 2014.

  1. Kev1807

    Kev1807

    Joined:
    May 4, 2014
    Posts:
    7
    Hi,

    my name is Kevin and I'm new to the forum and to unity.

    I'm pretty sure that this question has already been asked several thousand times, but I cannot find the solution...

    As I mentioned above, I'm a newbie with the engine, but I think it's great. However, I'm facing some problems:

    I'm currently walking through the tutorial of burgzerg (I think most of you know it...)
    http://www.burgzergarcade.com/tutorials/game-engines/unity3d/006-unity3d-tutorial-melee-combat-23

    and with the second part of the "melee tutorial" I have some trouble:

    The Tutorial goes like Script in C# for the Enemy to attack our "player"
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAttack : MonoBehaviour {
    5.     public GameObject target;
    6.     public float attackTimer;
    7.     public float coolDown;
    8.    
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.         attackTimer = 0;
    13.         coolDown = 2.0f;
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         if (attackTimer > 0)
    19.             attackTimer -= Time.deltaTime;
    20.        
    21.         if(attackTimer < 0)
    22.             attackTimer = 0;
    23.        
    24.            
    25.     }
    26.    
    27.     private void Attack () {
    28.         float distance = Vector3.Distance (target.transform.position, transform.position);
    29.        
    30.         Vector3 dir = (target.transform.position - transform.position).normalized;
    31.        
    32.         float direction = Vector3.Dot (dir, transform.forward);
    33.        
    34.         Debug.Log (direction);
    35.        
    36.         if (distance < 2.5f) {
    37.            
    38.             if (direction > 0) {
    39.                 PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
    40.                 eh.AddjustCurrentHealth (-10);
    41.             }
    42.         }
    43.        
    44.     }
    45.    
    46. }
    47.  
    I typed this from the screen, while watching the tutorial (in fact, I did as he himself, copy&paste it from another script, written before, the following) In his video all seems to be working, but not with my scripts:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerAttack : MonoBehaviour {
    6.     public GameObject target;
    7.     public float attackTimer;
    8.     public float coolDown;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         attackTimer = 0;
    14.         coolDown = 2.0f;
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (attackTimer > 0)
    20.             attackTimer -= Time.deltaTime;
    21.  
    22.         if(attackTimer < 0)
    23.             attackTimer = 0;
    24.  
    25.         if (Input.GetKeyUp (KeyCode.F)) {
    26.                     if (attackTimer == 0)
    27.                     Attack ();
    28.                     attackTimer = coolDown;
    29.                 }
    30.             }
    31.        
    32.         private void Attack () {
    33.         float distance = Vector3.Distance (target.transform.position, transform.position);
    34.  
    35.         Vector3 dir = (target.transform.position - transform.position).normalized;
    36.  
    37.         float direction = Vector3.Dot (dir, transform.forward);
    38.  
    39.         Debug.Log (direction);
    40.  
    41.         if (distance < 2.5f) {
    42.  
    43.                     if (direction > 0) {
    44.                             EnemyHealth eh = (EnemyHealth)target.GetComponent ("EnemyHealth");
    45.                                 eh.AddjustCurrentHealth (-10);
    46.                         }
    47.             }
    48.    
    49.     }
    50.  
    51. }

    I cannot make this work.
    I don't get an debug error or something like that. This script just seems not to affect the second one PlayerAttack.
    It's probably easy, but I didn't spot any difference while watching the video again and again it totally looks the same to me....

    edit: I just realised that not receiving any error messages should indicate that nothing is wrong with my code, am I right? I fixed one or two typos in the quote and checked whether they were in the actual Code, which they weren't so I still have this issue and seem unable to fix it...

    best wishes,
    and thanks in advance for your answers,

    Kevin
     
    Last edited: May 4, 2014
  2. Kev1807

    Kev1807

    Joined:
    May 4, 2014
    Posts:
    7
    that's disappointing:

    I have rebuilt the whole scene (which was luckily not that much :-D) and it still doesn't work.
    So I guess I hit a serious bug here? Now I'm at university and will probably check later, if I missed any other things.

    Any help or hint would be appreciated,
    so far, nothing new,

    Kevin
     
  3. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    You're not setting the target field in either script. Are you supposed to set it from the Editor, and are you in fact setting it to whatever value you're supposed to set it?
     
  4. Kev1807

    Kev1807

    Joined:
    May 4, 2014
    Posts:
    7
    I set the target of the enemy by dragging the player into the target field in the inspector of the enemy.
    WHen I'm home I'll try to assign the target in the script so that right at the beginning the enemy has the target...

    I did the same as in the tutorial (he also drags the Player into this field ...)

    thanks for your answer :)
     
  5. Kev1807

    Kev1807

    Joined:
    May 4, 2014
    Posts:
    7
    So I tried it and it still does the same thing... The script works, but the attacking or better the damaging is not done.

    Here is my modified script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAttack : MonoBehaviour {
    5.     public GameObject target;
    6.     public float attackTimer;
    7.     public float coolDown;
    8.  
    9.     void Start() {
    10.         attackTimer = 0;
    11.         coolDown = 2.0f;
    12.         target = GameObject.FindGameObjectWithTag ("Player");
    13.     }
    14.  
    15.     void update() {
    16.         if(attackTimer > 0)
    17.             attackTimer -= Time.deltaTime;
    18.    
    19.         if(attackTimer<0)
    20.                     attackTimer = 0;
    21.         if(attackTimer == 0){
    22.             Attack();
    23.             attackTimer=coolDown;
    24.  
    25.             }
    26.     }
    27.  
    28.     private void Attack(){
    29.         float distance = Vector3.Distance(target.transform.position, transform.position);
    30.  
    31.         Vector3 dir = (target.transform.position - transform.position). normalized;
    32.  
    33.         float direction = Vector3.Dot(dir, transform.forward);
    34.  
    35.         if(distance < 2.5f){
    36.             if(direction > 0){
    37.                 PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
    38.                 ph.AddjustCurrentHealth (-10);
    39.                 }
    40.             }
    41.         }
    42. }
    43.  
    44.  
    I did only change a few things:

    1) I changed the Attack() function so that it won't look for a target by waiting for me to drag&drop it, but to make as target the "Player" Tagged GameObject (which is the player itself ...)


    If anyone can possibly give me a hint on how to fix this issue I'd really appreciate it.
    It must not necessarily be with this script, but might as well be with another idea ...

    PS I tried commenting several things out

    such as the line

    "AttackTimer stuff in void update" which did not change anything, but the AttackTimer was not set (so everything cool here)

    I than tried to modify the Attack()
    by commenting out, the direction and distance aspects, which did not change anything either.

    So I think it's most probably not an issue with these things, but with
    Code (csharp):
    1. PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
    2.                 ph.AddjustCurrentHealth (-10);
    3.  
    So what other way of decreasing the PlayerHealth in the script PlayerHealth

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerHealth : MonoBehaviour {
    5.    
    6.     public int maxHealth = 100;
    7.     public int curHealth = 100;
    8.    
    9.    
    10.    
    11.     public float healthBarLength;
    12.    
    13.    
    14.     // Use this for initialization
    15.     void Start () {
    16.        
    17.         healthBarLength = Screen.width /2;
    18.        
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.        
    24.         AddjustCurrentHealth(0);
    25.        
    26.     }
    27.    
    28.     void OnGUI(){
    29.  
    30.         GUI.Box (new Rect(10,10, healthBarLength, 20), curHealth + "/" + maxHealth);  
    31.        
    32.     }
    33.    
    34.     public void AddjustCurrentHealth(int adj){
    35.        
    36.         curHealth += adj;
    37.        
    38.         if(curHealth <0)
    39.             curHealth = 0;
    40.        
    41.         if(curHealth > maxHealth)
    42.             curHealth = maxHealth;
    43.        
    44.         if(maxHealth <1)
    45.             maxHealth = 1;
    46.        
    47.        
    48.         healthBarLength =(Screen.width /2) * (curHealth / (float)maxHealth);
    49.        
    50.     }
    51. }
    could be found?


    I hope you can answer these questions,

    best wishes and thanks again in advance for response and help
    Kevin
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Maybe a typo from recreating the scripts, but in your last post, the update function is not called in the enemie's script because it's called update and not Update. It is considered as a completely different function and will not be called every fram by the engine.
     
  7. Kev1807

    Kev1807

    Joined:
    May 4, 2014
    Posts:
    7
    awkward, I checked so seriously but that one slipped my eye ...

    Now it's working (though I checked, the version before the void update was capitalized ...)

    I guess I would've tried and looked over again and again and well probably never have realized what actually was wrong ...

    (wrong is I guess not exactly correct, more it's the wrong function that is called ...)

    thanks for your answer:)

    I hope things like that won't happen any more