New developer here, looking to do some raycasting and controlling audio positioning in 3D space based on the raycasts' interactions with other objects.
I've got the raycast 'casting' and able to report back where it collides with other objects and managed to get audio to play back at that raycast point using AudioSource.PlayClipAtPoint()
However, PlayClipAtPoint
doesn't allow me to change the audio's 3D attenuation settings as a one shot audio object is generated and then removed immediately and I can't find any parameters to control this new object apart from a volume 0-1 float value.
I came across this post by Aldonaletto:
function PlayClipAt(clip: AudioClip, pos: Vector3): AudioSource; {
var tempGO = GameObject("TempAudio"); // create the temp object
tempGO.transform.position = pos; // set its position
var aSource = tempGO.AddComponent(AudioSource); // add an audio source
aSource.clip = clip; // define the clip
// set other aSource properties here, if desired
aSource.Play(); // start the sound
Destroy(tempGO, clip.length); // destroy object after clip duration
return aSource; // return the AudioSource reference
}
But can't seem to get the local function to work in Unity as it complains about not using C# version 7 or above. If I upgrade the C# version in Visual Studio, I get compiler errors on Unity's side.
I'm a little bit stuck, and would appreciate some advice! Some code below, just not sure what to put into the result of the if statement to get this to work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class raycasting : MonoBehaviour
{
public float raycastlength = 3;
public AudioClip playthis;
public float ER_Volume = 1F;
// Use this for initialization
void Start()
{
//Debug.Log("I'm a sphere, yo!");
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
Ray forwardray = new Ray(transform.position, Vector3.forward);
//draw the raycast in scene view
Debug.DrawRay(transform.position, Vector3.forward * raycastlength, Color.white);
if (Input.GetMouseButtonDown(0))
{
//Debug.Log("We pressed the fire button!");
if (Physics.Raycast(forwardray, out hit, raycastlength))
{
if (hit.collider.tag == "evil")
{
Debug.Log("Raycast Hit!");
Debug.Log("Distance to object:" + hit.distance);
//Debug.Log("Object hit point:" + hit.point);
//play a sound using playclipatpoint, but this doesn't allow us to change attenuation settings
AudioSource.PlayClipAtPoint(playthis, hit.point, ER_Volume);
}
}
}
}
}