Description
The PlaySound
method plays a specified sound on the GameObject
. The sound will follow the position of the GameObject
as it moves. This method is useful for adding audio effects that are spatially linked to a game object, such as footsteps, engine noises, or ambient sounds.
Usage
To use the PlaySound
method, you need to provide a SoundEvent
that defines the sound to be played and a Vector3
that specifies the position offset from the GameObject
's current position. The method returns a SoundHandle
, which can be used to control the sound after it has started playing, such as stopping it or adjusting its volume.
Example
// Example of using PlaySound method
// Assume 'gameObject' is an instance of GameObject
// Assume 'soundEvent' is a valid SoundEvent
Vector3 offset = new Vector3(0, 0, 0); // No offset
SoundHandle soundHandle = gameObject.PlaySound(soundEvent, offset);
// Use the soundHandle to control the sound if needed
// soundHandle.Stop(); // To stop the sound
// soundHandle.SetVolume(0.5f); // To set the volume to 50%