My enemy doesn't die when my player attacks, when I try to click space bar, the player is hitting the enemy, but the enemy only acknowledges the damage after five or more hits, any advice?
This is my first time making a game, I've scrapped together a patchwork of code, and I've taken my notes. When I tried to put the code together though, everything seemed to be working. The animation transitions were good, my character's controls responded well to jumping and horizontal movement. But when it came to the health and damage system, when the character walks up to the enemy and hits the supposed hitbox, it takes a few extra hits-- more than necessary-- to get the enemy//gameObject to actually disappear. Are there any fixes to this?
Here's my script:
using System.Collections;
using System.Collections.Generic
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
private float moveInput;
private Rigidbody2D rb;
private Animator anim;
private string midjump;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
private bool facingRight = true;
private float timeBtwAttack;
public float startTimeBtwAttack;
public Transform attackPos;
public LayerMask whatisEnemies;
public float attackRange;
public int damage;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
if ((Input.GetKeyDown(KeyCode.W)) && midjump == "n")
{
rb.velocity = new Vector3(0, 7, 0);
midjump = "y";
}
if (rb.velocity.y == 0)
midjump = "n";
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetKeyDown(KeyCode.W))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
} //jump
if (Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("isAttacking", true);
}
else
{
anim.SetBool("isAttacking", false);
} //attack
if (timeBtwAttack <= 0)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Collider2D[]enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatisEnemies);
for (int i = 0; i < enemiesToDamage.Length; i++) //this is a loop
{
enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
}
}//Damageee
timeBtwAttack = startTimeBtwAttack;
}
else
{
timeBtwAttack -= Time.deltaTime;
}//Attack delay
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos.position, attackRange);
} //attackRange vis
private void FixedUpdate() // This is horizontal movement
{
float moveInput = Input.GetAxisRaw("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
//Vhat is flip
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
And here's the script for the enemy;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int health;
public GameObject bloodEffect;
public void TakeDamage(int damage)
{
health -= damage;
Instantiate(bloodEffect, transform.position, Quaternion.identity);
}
void Update()
{
if (health <= 0)
{
Destroy(gameObject);
}
}
}
Answer by xxmariofer · Nov 18, 2020 at 10:22 AM
your issue is most likely with the timeBtwAttack, you are starting the attack animation even when the timeBtwAttack is not 0. move the animation code inside the if(timeBtwAttack)
Your answer
Follow this Question
Related Questions
[Solved] I can't call Method from Referenced Script 1 Answer
Turning player toward enemy after bool is true/button is pressed 1 Answer
How can I get my enemy to hurt/kill player 0 Answers
Enemy Isn't Hurting Player in Survival Shooter 0 Answers
Unity's 2d shooting can not be found:unityの2dシューティングがありません 0 Answers