Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

My Melee Attack in 2D hits MANY times

Discussion in 'Scripting' started by unity_8g7XwYU6kpaB-Q, Aug 23, 2019.

  1. unity_8g7XwYU6kpaB-Q

    unity_8g7XwYU6kpaB-Q

    Joined:
    Aug 17, 2019
    Posts:
    9
    Currently I am trying to implement combat into my game. My problem is, no matter what I do, the attack hits the enemy for 25 each hit all the way down to -1950 hp with one kick. If you could help me, I would appreciate it so much. I will include the scripts for the player and for the enemy below.

    Player Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class PlayerUnit : NetworkBehaviour
    7. {
    8.    
    9.     public float speed;
    10.     public int health;
    11.     public int damage;
    12.     public Transform attackPos;
    13.     public float attackRange;
    14.     public LayerMask whatIsEnemies;
    15.     public GameObject playerCamera;
    16.     private Rigidbody2D myRigidbody;
    17.     private Vector3 displacement;
    18.     private Animator animator;
    19.  
    20.    
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         health = 100;
    25.         animator = GetComponent<Animator>();
    26.         myRigidbody = GetComponent<Rigidbody2D>();
    27.         GetComponent<NetworkAnimator>().SetParameterAutoSend(0, true);
    28.         if (isLocalPlayer == true)
    29.         {
    30.             playerCamera.SetActive(true);
    31.         }else
    32.             {
    33.                 playerCamera.SetActive(false);
    34.             }
    35.        
    36.     }
    37.    
    38.  
    39.  
    40.     // Update is called once per frame
    41.     void FixedUpdate()
    42.     {
    43.        
    44.         if (isLocalPlayer)
    45.         {
    46.         displacement = Vector3.zero;
    47.         displacement.x = Input.GetAxisRaw("Horizontal");
    48.         displacement.y = Input.GetAxisRaw("Vertical");
    49.         UpdateAnimation();
    50.         MoveCharacter();
    51.         if (Input.GetKeyDown(KeyCode.K))
    52.             Kick();
    53.         }
    54.     }
    55.    
    56.     void UpdateAnimation()
    57.     {
    58.     if (displacement != Vector3.zero)
    59.     {
    60.         MoveCharacter();
    61.         animator.SetFloat("MoveX", displacement.x);
    62.         animator.SetFloat("MoveY", displacement.y);
    63.         animator.SetBool("Moving", true);
    64.     }else{
    65.         animator.SetBool("Moving", false);
    66.     }
    67.     if (Input.GetKeyDown(KeyCode.K)) {
    68.         //kick animation called
    69.         animator.SetBool("Kicked", true);
    70.     }else{
    71.         animator.SetBool("Kicked", false);
    72.     }
    73.     }
    74.    
    75.     void MoveCharacter()
    76.     {
    77.        
    78.         myRigidbody.MovePosition(
    79.             transform.position + displacement * speed * Time.deltaTime
    80.             );
    81.     }
    82.    
    83.     void Kick()
    84.     {      
    85.             Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
    86.             for (int i = 0; i < enemiesToDamage.Length; i++) {
    87.                 enemiesToDamage[i].GetComponent<EnemyUnit>().TakeDamage(damage);
    88.                 Debug.Log(enemiesToDamage.Length);
    89.             }
    90.     }
    91.  
    92. }

    Enemy Script:
    Code (CSharp):
    1.      using UnityEngine;
    2.      using UnityEngine.Networking;
    3.      using System.Collections;
    4.      using System.Collections.Generic;
    5.    
    6.      public class EnemyUnit : NetworkBehaviour {
    7.          public int health;
    8.          public bool enter = true;
    9.          public int normalId = 0;
    10.          public int dummyId = 0;
    11.          // Use this for initialization
    12.          public void Start () {
    13.  
    14.              health = 100;
    15.          }
    16.        
    17.          // Update is called once per frame
    18.          public void Update () {
    19.    
    20.          }
    21.        
    22.        
    23.          public void OnTriggerEnter2D(Collider2D other)
    24.          {
    25.              if (enter){
    26.                  RetrieveId(other.GetInstanceID());
    27.                  Debug.Log(other.GetInstanceID());
    28.              }
    29.            
    30.          }
    31.        
    32.          public void RetrieveId(int id)
    33.          {
    34.              Debug.Log(id);
    35.              dummyId = id;
    36.              Debug.Log(dummyId);
    37.          }
    38.        
    39.         public void TakeDamage(int damage)
    40.         {
    41.             if (normalId != dummyId)
    42.             {
    43.                 health -= damage;
    44.                 Debug.Log(health);
    45.                 normalId = dummyId;
    46.             } else{
    47.                 normalId = 0;
    48.             }
    49.         }  
    50.      }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Input.GetKeyDown() is intended to be used only in Update().

    It compares the state of the key this Update() frame to the state of the key the previous Update() frame and returns true when it has transitioned to down.

    If you use it in FixedUpdate() its behavior is undefined. It may miss keypresses entirely, or give you multiple instances of a key going down.

    I recommend moving the if check to Update() and then set a bool variable true, which you can test in FixedUpdate() and then clear it once you have done the kick.

    For more information on the Unity lifecycle, check here:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    Note how the physics block/loop does NOT include the Input block.

    Note how the Input block executes exactly as many times as the Update step.
     
    unity_8g7XwYU6kpaB-Q likes this.
  3. unity_8g7XwYU6kpaB-Q

    unity_8g7XwYU6kpaB-Q

    Joined:
    Aug 17, 2019
    Posts:
    9
    You are the best, I totally forgot I changed the Update to Fixed Update. Thank you so much.
     
    Kurt-Dekker likes this.