I’m making a stealthy kinda game where you play as a kid and you have to make it down stairs without waking your parents, the player will be in 3rd person (teamate currently working on making the character).
How would I be able to make a toy play a sound (adding a sneak level too) when the kid steps on it?
I’m thinking a OnCollision or OnTrigger
C# please. I’m also a beginner to programming.
-
Add a primitive collider ( e.g box collider ) to your toy game object.
-
Check the isTrigger property on this collider
-
Add an audio source to the toy game object ( and add the audio clip to this component )
-
Add the following script to your toy game object .
-
Make sure the player game object is tagged as ‘Player’ in the inspector.
using UnityEngine;
using System.Collections;public class playSound : MonoBehaviour {
private AudioSource source; // Use this for initialization void Start () { source = GetComponent<AudioSource>(); } void OnTriggerEnter(Collider otherObj) { if (otherObj.tag == "Player") { if(source!=null) source.Play(); } else source.Stop(); }
}