- Home /
The question is answered, right answer was accepted; Duplicate Question; Asking for Script
Picking up an object with a key.
Basically im making a 3D game and I need a script so when I enter a Collider, I can then press E to make the object dissapear. I also want to have a AudioClip play once I press E too. I just really need a script not a reference because I always fail at those. Please anyone who can help. Cheers
Unity Answers is not here to ask for others to do your work. Check out elance.com for that.
I just can't get it right. Ive tried and tried but i just can't get it to work. Ive tried everything even raycasting but it all fails.
I'm sure the OP will say 'uhuh man, this is for real', but my original comment was based on the fact that this exact same question is asked all the time (re: press E in a collider to interact w/ object and playsound). So often, with the exact same details, that it just feels like a homework assignment from somewhere out there in Game Program$$anonymous$$g 101 land.
OHHHH ok because im like dude, im in middle school. Wow i didn't even know that.
Answer by SkaredCreations · Mar 23, 2014 at 06:37 PM
I'll write the code now, it's really easy so I don't know why you're having so much issues, but if you want to use Unity and you haven't experience at all in programming then you should really find a programmer as partner for your game or learn some programming basics else you will be here on Answers most of your time :)
Here is:
public AudioClip myAudioClip;
bool isInside = false;
void OnTriggerEnter (Collider other) {
isInside = true;
}
void OnTriggerExit (Collider other) {
isInside = false;
}
void Update () {
if (Input.GetKeyUp(KeyCode.E) && isInside) {
audio.PlayOneShot(myAudioClip);
Destroy(gameObject);
}
}
This code presumes that your collider is set as Trigger and you have an AudioSource component attached to the gameobject. The script should be attached to your target collider, not your player of course.
Thank you the script worked beautifully I appreciate the time and effort you took to make this script and have a nice day! - Pig