- Home /
Play Audio-Clip On Ray Casting Collision
Howdy, would anybody be able to tell me how adapt my existing script so that an Audio-Clip will play on the ray cast collision. I've spent all day butchering my existing code trying to achieve this but I've got nowhere with it. Here it is in working order, minus any audio elements:
function Update()
{ var hit : RaycastHit;
//check if we're colliding if(Physics.Raycast(transform.position, Vector3(transform.forward.x, -0, transform.forward.z), hit, 10) ) { //...with a plane if(hit.collider.gameObject.tag == "orangeplane") { //change colour! hit.collider.gameObject.animation.Play("orange"); } } }
Any suggestions on how to achieve this will be greatly appreciated, Will.
Answer by qJake · May 02, 2010 at 08:25 PM
Read the AudioSource script reference.
Add an Audio Source component to the object you want to emit the sound from. Then, reference the Audio Source from your code, and call the function PlayOneShot();
. This will play the sound file you've loaded into the Audio Source once in your scene at the object's position.
Your answer
Follow this Question
Related Questions
Play animation with Raycast 1 Answer
Play sound on animation event? 3 Answers
Collision position - Improving on the 'grounded' mechanic 2 Answers
How could it be done "Cooldown" for a shield ? 2 Answers
RAYCAST KNOWLEDGE HELP!!! 1 Answer