- Home /
Play Animation OnTriggerEnter
I have a door script that is supposed to play an animation when I walk in the trigger and then also play a door opening sound. I have the same thing but except it is OnTriggerExit and it plays the sound and lays a close animation. The problem is, is that the sound is playing only on the trigger functions, but none of the animations are playing.
#pragma strict
var Door : GameObject;
var OpenSound : AudioClip;
var CloseSound : AudioClip;
function OnTriggerEnter(Other : Collider)
{
if(Other.gameObject.tag == "Player")
{
Door.animation.Play("SlidingDoor_Open");
audio.clip = OpenSound;
audio.Play();
}
}
function OnTriggerExit(Other : Collider)
{
if(Other.gameObject.tag == "Player")
{
Door.animation.Play("SlidingDoor_Close");
audio.clip = CloseSound;
audio.Play();
}
}
Did you make sure that the animations were working beforehand ? If the sounds are getting called then the problem isn't the trigger.
Does your door game object has animations ? You need to check that in the inspector.
a) check object is not static - made this mistake in the past b) look into layering the a$$anonymous$$ations in the start function.
i will have a look later at my project about layers and post a snippet of code that does what you need. email me at klarax (at) gmail (dot) com if not heard from me. i have a tendency to forget :P
Answer by LazyDog_Rich · May 21, 2014 at 03:42 PM
I Think this should work
pragma strict
var door : Animation;
var OpenSound : AudioClip;
var CloseSound : AudioClip;
function OnTriggerEnter(Other : Collider)
{
if(Other.gameObject.tag == "Player")
{
door.Play("SlidingDoor_Open");
audio.clip = OpenSound;
audio.Play();
}
}
function OnTriggerExit(Other : Collider)
{
if(Other.gameObject.tag == "Player")
{
door.Play("SlidingDoor_Close");
audio.clip = CloseSound;
audio.Play();
}
}
Answer by TristoWestside · Nov 06, 2016 at 02:52 PM
can someone please repost this in c#? im sure im just making one simple mistake. but when i go back to say "doorClose" is quickly loops between say " close and open". ?
btw i did not get the code from this question. this is my code here.
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public Animator anim;
public float speed;
public float idleSpeed;
void Awake()
{
anim = GetComponent <Animator> ();
speed = 5f;
idleSpeed = 0f;
}
void Update ()
{
if (Input.GetMouseButton (0))
{
transform.Translate (Vector3.forward * idleSpeed * Time.deltaTime);
anim.SetBool ("Idle", true);
}
else
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
anim.SetBool ("Walk", true);
}
}
}
Your answer
Follow this Question
Related Questions
Trying to play other objects' sound and animations from Raycast hit 1 Answer
How to Play an animation state on an animator controller on collision? 1 Answer
Audio. How to play a sound file when triggered by collision? 3 Answers
Plane flyby triggering/help 1 Answer
Starting animation with collision 0 Answers