- Home /
Help Playing "Sound Clip" on RayCastHit? (RayCast help?)
Hello,
I am currently learning how RayCasting works and I need some help/example scripts. (I use Java.) Basically, I am using this script below to create a "RayCast" in front of my player. I am trying to make a script that plays an audio clip (one shot) when this "RayCast" hits a gameObject called "Monster." If anybody can help me out with this... I would appreciate it A TON! Thanks so much for your help!
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 100)) {
print ("There is something in front of the object!");
}
}
Answer by gregzo · Jul 26, 2013 at 10:10 PM
Hi!
If you look a little further down in the docs concerning raycast, you'll find that you can pass a RaycastHit object as parameter to the raycast. Here's your script, adapted :
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
var hitInfo : RaycastHit;
if (Physics.Raycast (transform.position, fwd, hitInfo, 100)) {
print ("There is something in front of the object!");
print ("that something's name is "+hitInfo.transform.name );
}
}
Check out the RaycastHit struct in the docs.
Playing a oneshot clip should be fine after that ( audio.PlayOneShot ( clip ); ).
So.. Basically... You're saying this script will play a One Shot Audio Clip when hit by the RayCast? Are you sure that's right?
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
var hitInfo : RaycastHit;
if (Physics.Raycast (transform.position, fwd, hitInfo, 100)) {
print ("There is something in front of the object!");
print ("that something's name is "+hitInfo.transform.name );
( audio.PlayOneShot ( clip );
}
}
Not quite... In 3 steps : 1) You raycast and check if the ray hit 2) If it has hit, you check what it has hit 3) If it's a monster, play the sound
How you check if it's a monster is up to you : you could have a monster layer, or a List of all curent monsters, or you could use GetComponent to see if the GameObject that has been hit has a script of type $$anonymous$$onster attached to it, etc...
Could you modify this C# script to play a sound clip ins$$anonymous$$d of destroying the object "$$anonymous$$onster?"
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour
{
RaycastHit hit;
void Update()
{
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, $$anonymous$$athf.Infinity))
{
if(hit.collider.gameObject.name == "$$anonymous$$onster")
{
Destroy(hit.collider.gameObject, 1);
}
}
}
}
That script works... I've tested it... If I could just change "destroy" to "play sound"... That would solve my problem...
Just change the Destroy line for audio.PlayOneShot ( myClip, myVolume );
myClip needs to be declare next to RaycastHit :
public AudioClip myClip;
and assigned in the inspector. I really wouldn't advise checking against names, though...
Answer by HanSoloYolo · Jul 23, 2017 at 08:26 PM
Guys I am really sorry to bother you on this old post, but I still have the same question without a clear answer. How would you modify this Destroy on RaycastHit script to Play an Audio file attached to the object you are hitting with Raycast?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchDestroyer : MonoBehaviour
{
Ray ray;
RaycastHit hit;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.touchCount > 0)
{
ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
if (Physics.Raycast (ray, out hit, Mathf.Infinity))
Destroy (hit.transform.gameObject);
}
}
}
To clarify, I want to be able to touch an object like a dog and have it bark, then touch a cat and have it meow without destroying it.
However, I would like to learn how to play a sound attached to an object AND THEN destroy it with a similar script.
Seeing both of these options will help me learn a lot about scripting and touch controls. I have spent over a week now trying to figure this out.