- Home /
Why is my character taking damage when attacking & why is it attacking multiple times per hit?
I use a trigger jump attack on my player. The problem is that when I touch the enemy once it takes 40 damage. My player also takes damage whenever I touch the enemy. MY player attack damge is set to 10
PlayerHealth
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 200, currentHealth;
int amount;
public Slider healthSlider;
Animator anim;
Player Player; //reference to Player Script
bool dead;
bool damage;
// Start is called before the first frame update
void Awake()
{
anim = GetComponent<Animator>();
Player = GetComponent<Player>();
currentHealth = startingHealth;
}
// Update is called once per frame
void Update()
{
}
public void TakenDmg(int amount)
{
damage = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
//anim.SetBool("Hurting", true);
//if (!damage) TODO: Fix Hurting Animation off
//{
// anim.SetBool("Hurting", false);
//}
if (currentHealth <= 0 ) {
Death();
}
}
public void Death()
{
dead = true;
Destroy(gameObject, .5f); //TODO: add in the hurt animation
Player.enabled = false; //stops player from moving when dead
SceneManager.LoadScene("MainMenu"); //TODO: Create a wait a few seconds before returning to mainmenu
}
} //playerHealth
PlayerAttack
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
public int attackdmg;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy")
{
other.GetComponent<OpossumHealth>().damageTaken(attackdmg);
}
}
}
OppossumHealth
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpossumHealth : MonoBehaviour
{
public int enemyHealth = 100, currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = enemyHealth;
}
// Update is called once per frame
void Update()
{
death();
}
public void death()
{
if (currentHealth <= 0)
{
Destroy(gameObject);
}
}
public void damageTaken(int attackdmg)
{
currentHealth -= attackdmg;
}
}
OppossumAttack
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpossumAttack : MonoBehaviour
{
public float timeattack = 1.5f;
public int amount;
GameObject PTarget;
PlayerHealth PlayerHealth;
bool playerRange;
float timer;
// Start is called before the first frame update
void Start()
{
PTarget = GameObject.FindGameObjectWithTag("Player");
PlayerHealth = PTarget.GetComponent<PlayerHealth>();
}
void OnCollisionEnter2D(Collision2D Opos)
{
if (Opos.gameObject == PTarget)
{
playerRange = true;
}
}
void OnCollisionExit2D(Collision2D Opos)
{
if (Opos.gameObject == PTarget)
{
playerRange = false;
}
}
// Update is called once per frame
void Update()
{
attackTime();
}
void attackTime()
{
timer += Time.deltaTime;
if (timer >= timeattack && playerRange)
{
attack();
}
//if (PlayerHealth.currentHealth <= 0)
//{
// if (PTarget.tag == "Player"){
// Destroy(gameObject, 2f);
//}
//}
}
void attack()
{
timer = 0f;
if (PlayerHealth.currentHealth > 0)
{
PlayerHealth.TakenDmg(amount);
}
}
}//opoAttack
Answer by Nikkq · Jun 06, 2019 at 11:26 AM
Hey mate I can't see your colliders but I suppouse there is one collider under your player character and at least one longer catched to the opossum. You do not define in your scripts whether you attack your enemy from top or bottom and that is why your enemy gets damage in first second of your video. Enemy probably gets inside collider when you jump and thats why he gets damage. I think you could use velocity direction to define if you jump on enemy from the top. The opossum script looks working fine.
Answer by tormentoarmagedoom · Jun 06, 2019 at 11:24 AM
Hello this post is so long, so much code to read and unserstand..
What I can tell to you is: This kind of errors, that are not functions not working or giving erreos. This kind of errors of logic of your script (that comunicates and share information between them), must be solved by you.
You must debug your code while running, cheking values of all varialbles and finding the moment when something its not as you expect to be. Find where, and why.
But is almost imposible to detect it only by reading the code.
Good luck! Bye!
Your answer
Follow this Question
Related Questions
Object: collider to floor, trigger to player 1 Answer
Distance destroy object 3 Answers
Patrolling AI doesn't see player if it's standing still 1 Answer
About trigger 3 Answers