OnTriggerEnter2D Not Detecting BoxCollider2D - Childed Object
I'm trying to do a prototype for a 2D stealth/platformer and I'm trying to get the super-basic mechanics set up. This is also my first time working with Unity, so I don't know a ton, but I do know a bit. What I have right now is a player that moves via script (it has a RigidBody2D and "Is Kinematic" is enabled), as well as a basic enemy (movement same as player). What I'm currently trying to do is get the enemy to notice the player at a certain distance using colliders. I have a Line of Sight Cone that is a child of the Enemy, which also has a RigidBody2D with "Is Kinematic" un-checked, as well as a Polygon Collider 2D for detection, and a script used for detecting the player if it enters the collider.
I've already tried to set the "Is Trigger"'s in a variety of ways (both on, player on only, and trigger on only), but no matter what I do, the OnTriggerEnter2D is never used when the two collide. I have no idea what I'm doing wrong, as everyone with a similar issue either didn't have Rigid Bodies, or didn't set their "Is Trigger", and I've done all of them.
I already have an "OnTriggerEnter2D" working on the player for detecting areas that they can hide, and that works perfectly fine! I even tried to set up the player's functional OnTriggerEnter2D to detect the Line of Sight Cone instead, but that one doesn't work either. Is there something weird that I'm doing wrong?
I considered that it was because the Line of Sight Cone is a child of the enemy, but the child still has a RigidBody2D, and holds the script. It also works when it collides with my hideable objects for the player, meaning the child thing shouldn't be the issue.
Here's the script I'm using:
using UnityEngine;
using System.Collections;
public class LineOfSightController : MonoBehaviour {
public bool isSeen = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D cols){
Debug.Log ("entered");
if (cols.tag == "Player") {
isSeen = true;
}
}
}
Answer by DecreeB · Nov 29, 2016 at 07:27 PM
After doing some testing, I've discovered it's something to do with the Prime31 CharacterController2D that is somehow breaking the ability for OnTriggerEnter2D to work properly with the player. I'm considering trying to script my own entire movement system, or just fine another. I might even try making a child specifically for the player to be detected. Either way, it'll be resolved eventually now that I found the cause of the issue.
Answer by josh15 · Nov 28, 2016 at 06:36 PM
i thing you should make it like this
void OnTriggerEnter2D (Collider2D cols)[
if (cols.gameObject.CompareTag("Player")) {
isSeen = true;
}
Yeah, I considered trying to rephrase the if statement, but the first "Debug.Log" that denotes when a trigger enters, regardless of the if statement, doesn't fire when it makes contact with the "Player". So, I would still have the issue of it not even entering the function as a whole, and from there I can change the if statement if necessary. Thanks for the comment, though :)
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D() function not working 1 Answer
OnTriggerEnter2D not working 2 Answers
Coin Magnet Collider Overlap 0 Answers
How to make a growing mesh 0 Answers
Jitter with Tilemap Collider's but no Jitter on standard Box Collider 1 Answer