- Home /
Question by
alymaryyyy · Sep 28, 2020 at 10:47 AM ·
c#unity 2d
Player attacks only once (loop thing)
I am completely new here. So the problem is my character only attacks once and that's it. I got 6 enemies and it only attacks one then the attack stops working.
And another problem the attack animation doesn't play at all.
If you got any idea on how to fix this let me know plz. Thank you! Unity C#
HERE IS MY CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
private float timeBtwAttack;
public float startTimeBtwAttack;
public Transform attackPos;
public LayerMask whatIsEnemies;
public float attackRange;
public int damage;
public Animator playerAnima;
void Start()
{
//Get the Animator attached to the GameObject you are intending to animate.
playerAnima = gameObject.GetComponent<Animator>();
}
void Update(){
if(timeBtwAttack <= 0){
//then you can attack
if(Input.GetKey(KeyCode.X)) {
Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);{
for(int i = 0; i < enemiesToDamage.Length; i++)
{
if (enemiesToDamage[i].GetComponent<enemy_move>())
{
enemiesToDamage[i].GetComponent<enemy_move>().TakeDamage(damage);
}
}
}
timeBtwAttack = startTimeBtwAttack;
} else {
timeBtwAttack -= Time.deltaTime;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cinemachine follow Player but also focus on trajectory 0 Answers
Brackeys EnemyAi Script 0 Answers