- Home /
Can anyone help me modify this script? (C#)
Hey all,
I need some help modifying this C# script. I've tried and tried and I just can't seem to get it... So basically, I am raycasting. Whenever the "RayCast" hits the gameObject called "Monster" (With an Audio Source attached to it), I want a ONE SHOT audio clip to play. The only problem with this script is it isn't playing a ONE SHOT audio file... When my "RayCast" is on the object, it plays the audio file tons of times. If anybody can help me out with this, that would be great! THANKS!
using UnityEngine;
using System.Collections;
public class Example2 : MonoBehaviour
{
RaycastHit hit;
void Update()
{
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity))
{
if(hit.collider.gameObject.name == "Monster")
{
hit.collider.gameObject.audio.Play();
}
}
}
}
Answer by aldonaletto · Jul 27, 2013 at 06:16 PM
A simple way to avoid repeated calls to Play is to check whether the sound is playing before play it (notice that collider allows direct access to the other properties like name and audio):
if(hit.collider.name == "Monster")
{
if (!hit.collider.audio.isPlaying) hit.collider.audio.Play();
}
But this would make the monster scream continuously while the camera is looking at him! If you want the sound to play only once, use a reference to the last monster seen and only play the sound when it changes:
using UnityEngine;
using System.Collections;
public class Example2 : MonoBehaviour
{
RaycastHit hit;
Collider lastMonster;
void Update()
{
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity))
{ // if it's a different monster...
if (hit.collider.name == "Monster" && hit.collider != lastMonster)
{
hit.collider.audio.Play(); // play the audio...
lastMonster = hit.collider; // and update lastMonster
}
}
}
}
Answer by tw1st3d · Jul 27, 2013 at 06:11 PM
using UnityEngine;
using System.Collections;
public class Example2 : MonoBehaviour
{
RaycastHit hit;
public float lastHit = 0.0, timeSpace = 1.0;
public bool wasHit = false;
void Update()
{
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity))
{
if(hit.collider.gameObject.name == "Monster" && !wasHit)
{
wasHit = true;
lastHit = Time.time;
hit.collider.gameObject.audio.Play();
}else if(wasHit){
if(Time.time > lastHit + timeSpace)
{
wasHit = false;
}
}
}
}
}
Try creating a time space required for the hit, and using a bool as an identifier as to whether or not enough time has passed.
Hey! Thanks for helping me out! I seemed to have gotten an error. Here it is: "Assets/1Scripts/Example2.cs(7,47): error CS0664: Literal of type double cannot be implicitly converted to type float'. Add suffix
f' to create a literal of this type."
Thanks!
public float lastHit = 0.0, timeSpace = 1.0;
should be
public float lastHit = 0.0f, timeSpace = 1.0f;
my bad
Your answer
Follow this Question
Related Questions
Help Playing "Sound Clip" on RayCastHit? (RayCast help?) 2 Answers
Raycast stop at distance 3 Answers
help with multiple sounds 0 Answers
Continuous RayCast on GetButtonDown 0 Answers
Raycasting doesn't seem to "hit" a Character Controller? [image] 2 Answers