Question by
nathanvj · Jul 02, 2016 at 02:41 PM ·
c#animationanimation controller
How to play 'open door' animation on only one door?
So I have this script attached to a collider (which is a child of the door).
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class animationOnKeyPress : MonoBehaviour {
public KeyCode key;
Text openDoorText;
public GameObject gameobjectWithAnimation;
public string triggerName;
public AudioClip audioAnimation;
AudioSource soundEffects;
// Use this for initialization
void Start () {
openDoorText = GameObject.Find ("Canvas/openDoorText").GetComponent<Text> ();
soundEffects = GameObject.Find ("SoundManager/SoundEffects").GetComponent<AudioSource> ();
}
void OnTriggerEnter(Collider collider) {
if (collider.tag == "Player") {
StartCoroutine (fadeIn ());
}
}
void OnTriggerExit(Collider collider) {
if (collider.tag == "Player") {
StartCoroutine (fadeOut ());
}
}
void Update () {
Color color = openDoorText.color;
bool done = false;
if (color.a >= 1 && Input.GetKey (key) && done == false) {
gameobjectWithAnimation.GetComponent<Animator> ().SetTrigger (triggerName);
StartCoroutine (fadeOut ());
soundEffects.PlayOneShot (audioAnimation);
done = true;
}
}
IEnumerator fadeIn () {
for (int i = 0; i < 11; i++) {
Color color = openDoorText.color;
color.a += 0.1f;
openDoorText.color = color;
yield return new WaitForSeconds (0.03f);
}
}
IEnumerator fadeOut () {
for (int i = 0; i < 11; i++) {
Color color = openDoorText.color;
color.a -= 0.1f;
openDoorText.color = color;
yield return new WaitForSeconds (0.03f);
}
}
}
Now it worked when I had one door. The door opened and the text (openDoorText = "Press F to open door") faded away and the door opened.
Now I added another door into my scene, using the same system. (door > child: collider with component script) but now when the player presses F to open the door, the other door opens automatically too!
Any solution WITHOUT using multiple Animation Controllers?
Thanks in advance
Comment