hello i want do a narrator to my game . when the Player triggers the collider i want play some sound.
But i dont want have much tags…
public void OnTriggerEnter(Collider other)
if (other.tag == "")
{
}
what is other methot to do that ?
hello i want do a narrator to my game . when the Player triggers the collider i want play some sound.
But i dont want have much tags…
public void OnTriggerEnter(Collider other)
if (other.tag == "")
{
}
what is other methot to do that ?
You could check if the object exists?
if (GameObject.Find("WhateverItsCalled") )
{
//it exists
}
Hope this helps.
Well, you can create custom script to detect whether to play sound or not.
//MusicOnPlayerCollision .cs
public class MusicOnPlayerCollision : Monobehaviour
{
public AudioSource audio;
}
public class PlayerScript: Monobehaviour
{
//...
void OnTriggerEnter(Collider other)
{
MusicOnPlayerCollision script = other.GetComponent<MusicOnPlayerCollision >();
if (script != null)//means sound should be played
{
script.audio.Play();//or whatever you want
}
}
}
Attach the MusicOnPlayerCollision script to the objects at which sound should be played on trigger and add AudioSource Component to the objects.
This may be what you want…
Maybe “layers” would work for you:
Or;
if all you care about is detecting a single specific object, you could do:
public class OnlyPlayerTriggers : MonoBehaviour {
public GameObject TriggerByObject;
void OnTriggerEnter(Collider other){
if(other.gameObject.transform == TriggerByObject.transform){
// Do something;
}
}
}
You then just drag the player object (or any object) into the “Trigger By Object” field in the inspector. Hope this helps.
I would personally just make a new class and handle it separately.
public class PhysicsInteractable : Monobehaviour
{
onCollisionEnter(Collision c)
{
//check if object also has this component so we know they should interact with each other.
if(c.gameobject.GetComponent<PhysicsInteractable>()){
//do stuff.
}
}
}
It’s also really easy to extend to your own liking without writing ambiguous code everywhere.
you can use tag, layers, names , transform, and even parent and child properties of the objects with the collidertrigger system remember that the value you get passed once you collide with the method OnTriggerEnter its a collider type, and you can acces several variables in it like its GameObject.name
for example…
Check this thread ive answered on a similar question
This could be a job for interfaces. It means you can have a whole bunch of different scripts that offer the same behaviour, as long as they implement the interface correctly.
Perhaps an interface that looked like this:
public interface ITriggerAudio
{
void PlayAudio();
}
Then your player script would say something like…
OnTriggerEnter(Collider other)
{
var audioObj = other.GetComponent<ITriggerAudio>();
if(audioObj != null)
{
audioObj.PlayAudio();
}
}
This would enable you to make the thing you have hit play the audio. Alternatively, you could make the player player the audio by stripping out the PlayAudio function from the interface and just using a function called on the player.