this script is in my player controller:
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "doorL")
{
hit.gameObject.animation.Play("door_L_open");
}
if(hit.gameObject.tag == "doorR")
{
hit.gameObject.animation.Play("door_R_open");
}
}
each door has its own tag, door_L and door_R. Each door has its own animation called, respectively, “door_L_open” and “door_R_open.”
Of course, i only want one sound to play once, not a sound for each door or it will be too muddled. Just one sound effect that sounds like 2 doors sliding open. So it only needs to watch for one door animation. Arbitrarily, I’ll pick door_L_open.
so, would this be the total and correct code?
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "doorL")
{
hit.gameObject.animation.Play("door_L_open");
audio.PlayOneShot("dooreffect");
}
if(hit.gameObject.tag == "doorR")
{
hit.gameObject.animation.Play("door_R_open");
}
}
–edit–
clearly not, as it generates this error:
“Assets/scripts/door.js(6,34): BCE0023: No appropriate version of ‘UnityEngine.AudioSource.PlayOneShot’ for the argument list ‘(String)’ was found.”
ok, so it looks like I need the “dooreffect” script instead, to tell it that when animation door_L_open plays, to play the sound. But I can’t seem to figure out how to write it properly. I have a different script that plays the sound when the door is hit, but as you hit the door it plays the sound a grillion times because you’re still touching the door. Best to, instead, make it play when the animation occurs. But how to do that?
this is what I have for the on collision
var DoorSound : AudioClip;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "door") {
AudioSource.PlayClipAtPoint(DoorSound,transform.position);
}
how do i reformat this so instead of onhit its onanimation played? Even this code is wrong somewhere as it generates “Assets/scripts/dooreffect.js(12,1): BCE0044: expecting }, found ‘’.”
…I just so wasn’t cut out to code lol.