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!
Cherno
3
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;
}
}
I think you need;
if(other.gameObject.tag == "Player"){
animation.Play ("DoorA");
}
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.
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 