- Home /
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?
Answer by Hellium · Apr 21, 2018 at 05:19 PM
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 : https://docs.unity3d.com/ScriptReference/AudioSource-time.html
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.
Your question was "How to start a sound not from it's start", and that's the way to do it. Detecting when an audioclip ends is another question. You will find answers to this questions on Unity Answers and on the forums.
Yeah, my question should have been more "How to loop a sound not from its start ?". Thanks anyway for your help.
Answer by anharx · Apr 22, 2018 at 07:17 AM
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);
}
Your answer
Follow this Question
Related Questions
Audio not coming out of speakers 1 Answer
AUDIO ISSUES - PlayOneShot... is cutting short... I think? 2 Answers
Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers
Sound Clip isn't playing when triggered. 1 Answer
Calling sound once in update 1 Answer