Hi guys
i have this code where i pick up an object
The sounds keeps repeating annoyingly and only plays normally when i let go of the object. I need the sound to play only when i pick it no matter how many times i do it.
var SpawnTo : Transform; //attaches an object to your character that you want the position of what you picked up to go to, so goes to my bulletspawn
var Object1 : Transform; //The object you want to translate and move
var dist = 5; //distance at which you can pick things up
var icon : GUITexture;
private var isHolding = false;
var audioShield: AudioSource;
function Start(){
icon.enabled = false;
}
function Update () {
if(Input.GetKeyDown(KeyCode.Q)){ //if you press 'q', q because closer to finger
if(Vector3.Distance(SpawnTo.position, Object1.position) < dist)
{
isHolding = !isHolding; //changes isHolding var from false to true
}
}
if(isHolding == true){
Object1.rigidbody.useGravity = false; //sets gravity to not on so it doesn't just fall to the ground
Object1.parent = SpawnTo; //parents the object
Object1.transform.position = SpawnTo.transform.position; //sets transform position
Object1.transform.rotation = SpawnTo.transform.rotation; //sets rotation
shield=GameObject.Find("CFX2_PowerAura");
Destroy(shield);
icon.enabled = true;
audioShield.Play();
}
else{ //if isHolding isn't true
SpawnTo.transform.DetachChildren(); //detach child (object) from hand
Object1.rigidbody.useGravity = true; //add the gravity back on so resets everything
icon.enabled = false;
}
}
//GameObject create other Gui texture drag my texture on that and then drag it on the icon variable in the inspector
//spawn to is the empty game object on my character
//object one is the actual object getting picked up