Collision question

At the end of this lesson, the instructor tests out the CrashDetector script and demonstrates the player bouncing on the circle collider 2D that is set to the player’s head.

However, that collider is checked as a trigger and thus doesn’t have collision. In my code, when I attempt to crash the player, I correctly get the OnTriggerEnter associated with the head circle collider, but the sprite floats through the ground until catching on the snowboard capsule collider. That all makes sense to me because that’s how we set up the collision, but somehow the instructor is getting collision on the head as well.

How is that possible? Please help.

6 Likes

Hey, I also noticed that. What I did was made a slightly smaller Circle Collider 2D within the one on the head made as a trigger, and also another (slightly larger) Circle Collider 2D in the shoulder area, both of these extra ones not set as triggers, so they are solid. It doesn’t have the same effect where it bounces or where his character doesn’t roll onto his side after, but it will make his head and the area around it solid.

1 Like

I have found a fix for this. The video really should be rerecorded as there seems to be a bug in unity with the isTrigger and the sprite shape, for me when the head trigger hits the ground it resets the position of the player object.

The fix is to make sure that the circle collider 2d is not set as a trigger then have the following code for the CrashDetector.cs file.

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

public class CrashDetector : MonoBehaviour
{
    CircleCollider2D playerHead;
    void Start()
    {
        playerHead = GetComponent<CircleCollider2D>();
    }
    private void OnCollisionEnter2D(Collision2D other) {
        if (other.gameObject.CompareTag("Ground") && playerHead.IsTouching(other.collider))
        {
            Debug.Log("I hit my friggin Head");
        }
    }
}

5 Likes

Hi @winSome,

Welcome to our community! :slight_smile:

Actually, Rick’s Barry should have dived into the ground instead of bouncing off. In later videos, you can see, that he doesn’t bounce anymore. Rick suspects that the surface effector made the head bounce in this case. Sometimes, there are undesired side-effects, and since we didn’t write the Collider2D classes or the surface effector class ourselves, we cannot prevent those side-effects.

However, since we know what the problem is (trigger collider bouncing off), we can simply filter for that specific case like Anthony did in his code.

4 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms