I could really use some help with this. I’m new to Unity scripting have previously been using just Visual Studio C# and I’m finding it difficult to get started.
Basically what I’m trying to do is have a door slide open when the player approaches it.
Now I’ve got my Capsule which is tagged as Player and I’ve got a Door which is tagged as MainDoor. Each of them has a collision box. The door has a Box Collider with Is Trigger ticked.
I’ve created my animation and called it DoorA and attached it to the Door object.
Now I’ve been trying to get the script working, at first I had it attached to the Door, then I had it attached to the player. What is confusing me is how to reference another objects collider and what it means when I’m calling a collider other?
Here is my code below:
using UnityEngine;
using System.Collections;
public class MainDoor : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider other) {
animation.Play ("DoorA");
}
// Update is called once per frame
void Update () {
}
}
Now at present it is attached to the Door. In pseudo code this is what I think I should be doing
When Doors trigger is hit by Player Collider play door animation.
But I’m unsure how to do it! Help would be appreciate, I’m a novice!
When I put a tag on the OnTriggerExit function I can see that it happens right away as soon as the game starts. Which suggest that the Player is leaving the trigger area on game launch. That doesn't make sense to me. This script by the way is attached to the door not the player object.
Here is my door script, fairly comprehensive but you can just pick the parts you really need from it. You might have to set your door animation import setting so that the opening and closing animations are Clamp Forever, so CrossFade works (if the player opens it while it is closing). Also consider editing the physics layer interaction in Project Settings → Physics so the door collider only interacts with player characters, or anything else that should block the door.
using UnityEngine;
using System.Collections;
public class Door : MonoBehaviour
{
private int state;//0 = closed/idle, 1 = opening, 2 = closing, 3 = open
private float openTimer;
private float closeTimer;
private float autoCloseTimer;
public bool blocked;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(state == 3) {
if(blocked == false) {
autoCloseTimer += Time.deltaTime;
}
else {
autoCloseTimer = 0.0f;
}
}
else {
autoCloseTimer = 0.0f;
}
if(autoCloseTimer >= 5.0f && blocked == false) {
ActivateDoor();
}
if(state == 1) {
openTimer += Time.deltaTime;
}
if(openTimer >= 1.5f) {
state = 3;
ResetTimer();
}
if(state == 2) {
closeTimer += Time.deltaTime;
if(blocked == true) {
animation.CrossFade("door1_open", 1.0f, PlayMode.StopAll);
state = 1;
ResetTimer();
}
}
if(closeTimer >= 1.5f) {
state = 0;
ResetTimer();
}
}
void OnTriggerStay() {
blocked = true;
ActivateDoor();
}
void OnTriggerExit() {
blocked = false;
}
public void ActivateDoor() {
if(state == 0) {
animation.Play("door1_open", PlayMode.StopAll);
state = 1;
ResetTimer();
return;
}
if(state == 1) {
return;
}
if(state == 2) {
animation.CrossFade("door1_open", 1.0f, PlayMode.StopAll);
state = 1;
ResetTimer();
return;
}
if(state == 3) {
animation.Play("door1_close", PlayMode.StopAll);
state = 2;
ResetTimer();
return;
}
}
void ResetTimer() {
openTimer = 0.0f;
closeTimer = 0.0f;
}
}
In the below line, “other” is the Collider of the triggering object.
void OnTriggerEnter(Collider other) {
So other.gameObject refers to the triggering gameObject. You could also be doing
other.rigidbody.velocity = someSpeedVector;
therefore you would be reaching the rigidbody of the triggering object. Check the link below, you can see what variables you can use with Collider component.
Also as a note, your player should have a rigidbody in order to trigger occur. You can see from the below link, when collisions are detected.
no, it should run once. I'm not sure why this is not working. While I think this should work as it is now, you can fix this, by defining another variable. It might be related to how you set up the colliders. if (other.gameObject.tag == "Player" && !isOpen) { animation.Play ("DoorA"); isOpen = true; }
well, I can not see any reason why you are handling this like that. OnTriggerEnter you can use anim.SetBool ("Open", true); OnTriggerExit; anim.SetBool ("Open", false); you would not need to run this on every update. Anyway, still in your script, if count goes up to 2 by a chance, then the door would not close. Put the following right under count++ Debug.Log(count); and under this also. count = Mathf.Max (0, count-1); you can check and see what's happening actually.
Thank you for the help with this. It's such a simple task but it just doesn't seem to want to work. I stripped my code back and use inputted the true and false you suggested within on on enter and on exit. However now when I run the game the door continuously opens and shuts, it's very strange.
Yeah okay this is very strange. I put a check on my code to see if the bool was being set to false in the OnTriggerExit and it's not. So basically my trigger for OnTriggerEnter is working fine and sets the bool to true. But it doesn't detect the player leaving the trigger area.
The above information was very helpful. As it turns out the problem with my code was that I had included a player controller which uses crouch. That crouch was kicking in when I approached the ceiling near the doors entrance and for whatever reason this was affective the collision and animation. I removed the crouch feature and it now works
When I put a tag on the OnTriggerExit function I can see that it happens right away as soon as the game starts. Which suggest that the Player is leaving the trigger area on game launch. That doesn't make sense to me. This script by the way is attached to the door not the player object.
– William_Goodspeed