hi, i wanted help with damage, so i just made a simple script, it’s just an image which damages the player when collides with the player, but for some reason the player takes damage of 2 hp instead of 1, and i can’t figure out why that happens and how to fix it
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageOnCollision : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
collision.gameObject.GetComponent<PlayerHealth>().TakeDamage(1);
}
}}
Nevermind i used debug.log to see what happens, and apparently when the player collides with the object “it collides twice”, i guess it is because i have 2 colliders on my player (one for player when it’s standing and another one when the player crouch), ill try make the second (crouch) collider disable while it’s not in usage if i can figure it out by myself xd
_
EDIT:
This fixed it.
if this helps someone, in void update, if im not pressing my crouch key, that 2nd (crouching collider) will be disabled, but when crouch key is pressed it gets enabled back, that’s all, since its, crouching the standing collider is already disabled and the 2nd collider that is for crouch will be ready.
_
this is what i put to disable the 2nd collider cus it’s not really being used:
if (Input.GetButton("Shift") == false)
{
collider2.enabled = false;
}
and then when i actually want to crouch, so it’ll be used
if (Input.GetButtonDown("Shift"))
{
crouch = true;
collider2.enabled = true;
}
(didn’t change get button up since as soon as i stop pressing crouch, it is not anymore pressed so it just disables again)
i don’t know if it is a good fix, but at least for what im doing, it is ok