- Home /
Can't get trigger collision detection to work
I'm working through Unity 3.x Game Development Essentials, currently on chapter 5. The goal is to get the door of an outpost to open and close and play the appropriate sound effects when the player enters the trigger zone. But this isn't working for me. The door does nothing.
I scaled and positioned the trigger zone of the outpost correctly and enabled "Is Trigger". I have this script attached to the outpost itself:
using UnityEngine;
using System.Collections;
public class TriggerZone : MonoBehaviour {
void Start () {
}
void Update () {
}
void OnTriggerEnter(Collider col){
if(col.gameObject.tag == "Player"){
transform.FindChild("door").SendMessage("DoorCheck");
}
}
}
Then I have this script attached to the door, which is a child of the outpost parent:
using UnityEngine;
using System.Collections;
public class DoorManager : MonoBehaviour {
bool doorIsOpen = false;
float doorTimer = 0.0f;
public float doorOpenTime = 3.0f;
public AudioClip doorOpenSound;
public AudioClip doorShutSound;
void Start () {
doorTimer = 0.0f;
}
void Update () {
if(doorIsOpen){
doorTimer += Time.deltaTime;
if(doorTimer > doorOpenTime){
Door(doorShutSound, false, "doorshut");
doorTimer = 0.0f;
}
}
}
void DoorCheck(){
if(!doorIsOpen){
Door(doorOpenSound, true, "dooropen");
}
}
void Door(AudioClip aClip, bool openCheck, string animName){
audio.PlayOneShot(aClip);
doorIsOpen = openCheck;
transform.parent.gameObject.animation.Play(animName);
}
}
I think there might be some kind of communication problem between the two objects..? Or are there errors in either of my scripts? I quadrupled checked them to the best of my ability and don't think I missed any steps. Thanks in advance.
I have the exact same problem with Trigger Collision Detection. I have the exact same Scripts as well...
I have tagged the First Person Controller as "Player" and still the door does not open.
Answer by MikeNewall · Feb 28, 2013 at 04:11 AM
Have you double checked that the player has a rigidbody on it, and it's actually tagged as "Player" ?
Ya, that was the problem. Didn't tag as Player. Embarrassing beginner mistake lol. Thank you!
$$anonymous$$ike you brilliant son of a gun! I missed tagging the First Person Controller as "Player"!
Answer by HPMPRacer · Aug 05, 2014 at 02:51 PM
@mikenewall - i have double and triple checked that the First Person Controller object is tagged as Player. Still no luck.
Also, I have tried Debug.Log("my message"); and nothing is being printed to the console. Not sure what i am missing here... The Code for raycasting works fine but for some reason my Box Collider is not detecting my character...