How to start a sound not from it's start ?

Hello, I don’t find anywhere how to do this if anyone has an idea?

How can I start a sound at 1s of it for example?

2 Answers

2

Have you tried:

audioSource.Play();
audioSource.time = /* time in seconds */ ;

Be aware that: On a compressed audio track position does not necessary reflect the actual time in the track
Compressed audio is represented as a set of so-called packets.
The length of a packet depends on the compression settings and can quite often be 2-3 seconds per packet.

Source : Unity - Scripting API: AudioSource.time

I'm searching more a way to loop a short sound from its middle after playing it first from the start (car horn honks). With this way, we need to update every time to check if the sound is finished, there is no function OnClipEnd and this can produce a break enter the end of this sound and next update. Else I'm going to make 2 sounds but I searching before if we can do it with code.

Try using audioclip.playoneshot(). For example if you only want the clip to play only when the player honks. Also make sure to add an audiosource component in the inspector and attach the audio clip to it

In the code sample the sound track only plays the when the cannonball is shot

 public int hitpoint = 20;
    public float minForce = 400.0f;
    public float maxForce = 700.0f;
    public AudioClip audioHit;
    public AudioClip audioShoot;
    public ParticleSystem particle;
    public float delayTime = 2.0f;

    private bool isActive = true;

    private void Awake()
    {
        this.GetComponent<Rigidbody>().AddRelativeForce(new Vector3 (0, GetRandomValue(), GetRandomValue()));
        this.GetComponent<AudioSource>().PlayOneShot(audioShoot);
    }