I want to stop the audio from playing once the gameobject is moved. the audio clip is a bit long so currently when I move the audio keeps playing until finishes.
Again I want it to stop completely when object is moved and if not moved to play looped as does at set time.
here is code I have so far
2D AND javascript
please tell me what I need or what to do or post corrected script so I can learn
#pragma strict
var animator : Animator;
var pow: AudioClip;
var hit: AnimationClip;
var idleAnimation : float = 0f;
var goneIdle : float = 5f;
var stay: AnimationClip;
var screenPoint : Vector3;
var offset : Vector3;
function Start() {
animator = GetComponent("Animator");
animation.clip = stay;
audio.clip = pow;
SetIdleTime();
}
function Update() {
idleAnimation += Time.deltaTime;
if(idleAnimation > goneIdle) {
SetIdleTime();
Debug.Log("stay");
// Play your animation
animation.Play("stay");
animator.SetBool("IdleBool", true);
//audio.Play();
//audio.PlayOneShot(pow);
if (!audio.isPlaying){
audio.clip = pow;
audio.Play();
}
}
}
function OnMouseDown () {
var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.GetMouseButton(0))
{
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
transform.position = curPosition;
idleAnimation = 0;
yield;
idleAnimation = 0;
animator.SetBool("IdleBool", false); // this is the line you add
yield;
}
}
function SetIdleTime() {
goneIdle = Random.Range(3, 16); // set value from 3 to 6
}