I want to activate a game object that plays a distant bullet impact sound and gun shot with a delay script when the raycast hit an object with a distance greater than 100 meters from the player. I do this so the other players know that an enemy is nearby or far away.
I know how to active the script but how do I put that in an if statement?
I have been figuring it out in my train ride home:
if (hit.distance > 100.0f)
{
impact.active=true:
}
JoMaHo
March 29, 2018, 6:45am
2
Have a read on this one twice or more:
Ok it seems I was doing fine but then when I play the intantiated audio file, it says “cannot play disable audio source” even though it is active
So when the raycast hits 100 meters, it instantiated impact obj with an audio source attached but it does not play it.
When I instantiate a distant hit effect with audio, it says, “Cannot play a disabled audio source” even though the audio source is enabled.
As as temporary solution, I set it to play an audio clip but it is not directional.
void pulse_rifle_fire()
{
if(Time.time >= timestamp)
{
pulse_rifle_muzzle.Play();
RaycastHit hit;
Instantiate(tracer, rifletip1.transform.position, rifletip1.transform.rotation);
timestamp = Time.time + timeBetweenShots;
if (Physics.Raycast(rifletip1.transform.position, rifletip1.transform.forward, out hit))
{
//print("Found an object - distance: " + hit.distance);
if (hit.collider.gameObject.tag == "Stone")
{
Instantiate(bullet, hit.point, Quaternion.identity);
//Instantiate(distimp, hit.point, Quaternion.identity);
if (hit.distance > dist){
distimp.Play();
}
}
if (hit.collider.gameObject.tag == "Armor")
{
Instantiate(bullet_metal, hit.point, Quaternion.identity);
//Instantiate(distimp, hit.point, Quaternion.identity);
if (hit.distance > dist){
distimp.Play();
}
}
if (hit.collider.gameObject.tag == "Water")
{
Instantiate(bullet_water, hit.point, Quaternion.identity);
//Instantiate(distimp, hit.point, Quaternion.identity);
if (hit.distance > dist){
distimp.Play();
}
}
if (hit.collider.gameObject.tag == "Flesh")
{
Instantiate(bullet_flesh, hit.point, Quaternion.identity);
//Instantiate(distimp, hit.point, Quaternion.identity);
if (hit.distance > dist){
distimp.Play();
}
}
if (hit.collider.gameObject.tag == "Wood")
{
Instantiate(bullet_wood, hit.point, Quaternion.identity);
//Instantiate(distimp, hit.point, Quaternion.identity);
if (hit.distance > dist){
distimp.Play();
}
}
}
}