In this lecture, one thing which was happening was that when the player was going through the finish line, the finish line was being triggered twice because there are two colliders on the player (capsule collider on the board and a circle collider on the face). I want to figure out how to fix this such that the finish line is triggered ONLY when the snow board’s capsule collider interacts with the box collider of the finish line.
I wrote this code thinking it might work (it hasn’t) and I’m very confused.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FinishLine : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
CapsuleCollider2D ABC = other.GetComponent<CapsuleCollider2D>();
if (this.GetComponent<Collider2D>().IsTouching(ABC))
{
Debug.Log("ADASDASDASDASD");
}
}
Firstly, this script is on the Finish Line game object. My understanding (or lack of) of what I have tried to do here is that the “other” parameter is the game object that’s colliding with the finish line (i.e. The Player). So, I created a variable of Type CapsuleCollider2D since that’s the collider I want to detect. I then used the “IsTouching” method to check if ABC was colliding with the collider of the Finish Line, and if it was, then print that gibberish.
But, the result that I’m getting out of it is this:
- The error arises when the box with a 2d box collider triggers the finish line. (I have no idea why).
- The gibberish is still being printed when the circle collider on the player’s head triggers the finish line.
Any advice will be helpful if I’m going wrong in my understanding or logic, and sorry for the long question.
Thank you!