How to Check for Collisions on Child Object

I was searching around for a solution to this, and everyone basically said to just put a script on the child. I can’t do this because my script disables the shield object when not blocking. Is there any way to check for a collision in child objects? Here’s my script,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBlock : MonoBehaviour
{
    public Animator animator;
    public Player player;
    public AttackBoy attackBoy;
    public bool blockHitter;
    private bool blocking;
    public GameObject shield;

    void Start()
    {
        attackBoy = GetComponentInParent<AttackBoy>();
        animator = GetComponentInParent<Animator>();
        player = GetComponentInParent<Player>();
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.S))
        {
            blocking = true;
            animator.SetFloat("blockPerc", 0.5f);
        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            blocking = false;
            if (!attackBoy.attackRoutine)
            {
                player.stopMove = false;
            }
        }
        if (blocking)
        {
            player.stopMove = true;
            shield.SetActive(true);
        }
        else
        {
            shield.SetActive(false);
            animator.SetFloat("blockPerc", 0f);
        }
    }
    private void OnTrigger2D(Collider2D collision)
    {
        if (collision.CompareTag("Enemy") || collision.CompareTag("AttackTrigger") && !blockHitter)
        {
            StartCoroutine(BlockHit());
        }
    }
    IEnumerator BlockHit()
    {
        blockHitter = true;
        yield return new WaitForSeconds(0.2f);
        animator.SetFloat("blockPerc", 1f);
        yield return new WaitForSeconds(1);
        blockHitter = false;
    }
}

I’m trying to make a shield, if anyone’s got any better suggestions I’m game, I cooked this up myself so it might not be optimal haha.

Thanks in advance

As long as you have a Rigidbody on the parent, the parent will receive collision events for any colliders on any children.

3 Likes

Okay, but I don’t want the main body receiving the collisions, just the shield

Well, I tried experimenting by putting it on the parent, making it a trigger so that it will only detect from the shield, setting it to not active when not blocking. It works fairly well? But the animation does not play properly, I’m not sure if that’s an issue with my blend tree or script? Here’s my script anyway, at blockPerc 0.5 it plays the basic block, at 1.0 it plays the block hit animation…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBlock : MonoBehaviour
{
    public Animator animator;
    public Player player;
    public AttackBoy attackBoy;
    public bool blockHitter;
    public bool blocking;
    public GameObject shield;
    public AudioSource[] blockHit;

    void Start()
    {
        attackBoy = GetComponent<AttackBoy>();
        animator = GetComponent<Animator>();
        player = GetComponent<Player>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            blocking = true;
            animator.SetFloat("blockPerc", 0.5f);

        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            blocking = false;
            animator.SetFloat("blockPerc", 0f);
            if (!attackBoy.attackRoutine)
            {
                player.stopMove = false;
            }
        }
        if (blocking)
        {
            player.stopMove = true;
            shield.SetActive(true);
        }
        else
        {
            animator.SetFloat("blockPerc", 0f);
            shield.SetActive(false);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("AttackTrigger") && !blockHitter)
        {
            StartCoroutine(BlockHit());
        }
    }
    IEnumerator BlockHit()
    {
        int randomSound = Random.Range(0, blockHit.Length);
        blockHitter = true;
        yield return new WaitForSeconds(0.2f);
        blockHit[randomSound].Play();
        animator.SetFloat("blockPerc", 1f);
        yield return new WaitForSeconds(1);
        blockHitter = false;
    }
}

Edit: I figured it out, not sure exactly what the issue was, but I fixed it lol