- Home /
Question by
Danny2036 · Apr 21, 2014 at 10:32 PM ·
animationgameobjectcolliderdoors
Opening a door when a button object has something it it's collider.
I am making a game in Unity where I want a door to open when I press a button. My button is a cube and when any object with a collider comes in contact with the collider of the cube I want to door to open. I have a script in the door that executes the animation. I have a script in the cube I use to try to call the door object. I tried making the door a child of the cube and some other thing but I don't know how to connect the two. The door does have an animation associated with it. The following are my two classes. using UnityEngine; using System.Collections;
public class Button : MonoBehaviour {
GameObject[] slideDoor = GetComponentInChildren();
private int count;
void OnTriggerEnter(Collider other){
count++;
}
void OnTriggerExit(Collider other){
if (count > 0) {
count--;
}
}
void Update(){
if (count > 0) {
slideDoor [0].anim.setBool("Open", count > 0);
}
}
}
using UnityEngine;
using System.Collections;
public class DoorAnimatio : MonoBehaviour{
public AudioClip doorSwishClip; // Clip to play when the doors open or close.
private Animator anim; // Reference to the animator component.
void Awake (){
anim = GetComponent<Animator>();
}
void Update (){
// Change this to work with a button instead of count
//anim.SetBool(DoorBool, buttondown);
// If the door is opening or closing...
}
public void playAnim(){
if(anim.IsInTransition(0) && !audio.isPlaying){
// ... play the door swish audio clip.
audio.clip = doorSwishClip;
audio.Play();
}
}
}
Comment