#3 Collision Bug

1 messages · Page 1 of 1 (latest)

tepid dock
#

Hi, I'm trying to make a 2d shooting minigame, but my 3 collisions are "bugging". All enemies has 10 hp, and head should do 5 damage, torso 3, and legs 1, but when I use 2 or 3 collisions it cannot detect anything, or sometimes I get torso or legs, but never head and I need to click a lot. First I did it with MouseOnOver events, but I tried to rewrite it with AI because I though that was the problem, now it uses Layers, enums and Raycast.
All code involved:

using UnityEngine;
using UnityEngine.InputSystem;

public class ShootingThings : MonoBehaviour
{
   
    [SerializeField] private int health = 10;

    public void ApplyDamage(int dmg)
    {
        //Debug.Log(health);
        //Debug.Log(dmg);
        health -= dmg;

        if (health <= 0)
            Destroy(gameObject);
    }
}

public class MouseBodyPart : MonoBehaviour
{
    public enum BodyPart { Head, Torso, Legs }


    [SerializeField] private BodyPart part;
    [SerializeField] private int damage = 1;
    private ShootingThings enemy;

    private void Awake()
    {
        enemy = GetComponentInParent<ShootingThings>();
        if (enemy == null)
        {
            Debug.LogError("No ShootingThings found in parent!");
        }
    }
    public void Hit()
    {
        enemy.ApplyDamage(damage);
    }
}

public class ClickShooter : MonoBehaviour
{
    [SerializeField] private Camera cam;
    [SerializeField] private LayerMask hurtboxMask;

    private void Awake()
    {
        if (cam == null) cam = Camera.main;
    }

    private void Update()
    {
        if (!Mouse.current.leftButton.wasPressedThisFrame) return;

        Vector2 world = cam.ScreenToWorldPoint(Mouse.current.position.ReadValue());
        RaycastHit2D hit = Physics2D.Raycast(world, Vector2.zero, 0f, hurtboxMask);

        if (!hit) return;

        var part = hit.collider.GetComponent<MouseBodyPart>();
        if (part != null)
            part.Hit();
    }
}
proven pawn
#

anyway, what steps you took to debug this.

#

also what exactly you mean here

when I use 2 or 3 collisions

#

enum is doing nothing right now too..

#

ShootingThings should just be called Health or something

#

other than that there is nothing really wrong with the scripts themselves functionally

tepid dock
# proven pawn anyway, what steps you took to debug this.

I used Debug.Log that when I click on one of the collision it log "i clicked it" or something like that. But sometimes I needed to click below the sprite or at the log to tell, I clicked Torso collision. I mean when I say 2-3 collision is that I disabled torso and leg, and head work but when I enable one of them again it breaks.

proven pawn
#

there is nothing wrong with the code itself and should log each part fine if you did

public void Hit()
   {
       Debug.Log($"Damaged {part} for {damage} damage.");
   }```
tepid dock
#

At the moment no animation, sprite name is idle_0 because that s the first frame of the spritesheet that I use but there's no animation related components on any of the game objects.

proven pawn
tepid dock
#

transform and the shooting things script, it has child gameobjects of idle_01 (sprite), head torso and leg
head has circle collider 2d, others have box collider 2d and the MouseBodyPart script, I tried and still the same sometimes I can hit torso and leg i will record a video maybe it s more useful than just writing it down.

proven pawn
# tepid dock

can you try it without a custom cursor ? maybe the hitpoint hotspot is not where you think it is..

tepid dock
#

yeah that was the issue I cant believe it 😄 thank you so much, btw can I change the hitpoint hotspot somehow or what it depends on? My cursor was around 15x17 png image

proven pawn
#

kinda strange you can only do it through code

tepid dock
#

ye im thinking of changing the cursor with script instead of using the unity setting

#

thank you