[ad_1]

New developer right here, seeking to do some raycasting and controlling audio positioning in 3D area based mostly on the raycasts interactions with different objects.
I’ve bought the raycast ‘casting’ and in a position to report again the place it collides different objects and managed to get audio to play again at that raycast level utilizing AudioSource.PlayClipAtPoint()

Nonetheless, PlayClipAtPoint would not permit me to alter the audio 3d attenuation settings as a one shot audio object is generated after which eliminated instantly and I am unable to discover any parameters to manage this new object other than a quantity 0-1 float worth.

I got here throughout this put up by Aldonaletto:

operate PlayClipAt(clip: AudioClip, pos: Vector3): AudioSource; {
   var tempGO = GameObject("TempAudio"); // create the temp object
   tempGO.rework.place = pos; // set its place
   var aSource = tempGO.AddComponent(AudioSource); // add an audio supply
   aSource.clip = clip; // outline the clip
   // set different aSource properties right here, if desired
   aSource.Play(); // begin the sound
   Destroy(tempGO, clip.size); // destroy object after clip length
   return aSource; // return the AudioSource reference
 }

However can not seem to get the native operate to work in Unity because it complains about not utilizing C# model 7 or above. If I improve the C# model in visible studio I get compiler errors on Unity’s aspect.

Little bit caught, would recognize some recommendation! Some code under, simply unsure what to place into the results of the if assertion to get this to work.

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class raycasting : MonoBehaviour
{
    public float raycastlength = 3;
    public AudioClip playthis;
    public float ER_Volume = 1F;

    // Use this for initialization
    void Begin()
    {
        //Debug.Log("I am a sphere, yo!");
    }

    // Replace is known as as soon as per body
    void Replace()
    {
        RaycastHit hit;
        Ray forwardray = new Ray(rework.place, Vector3.ahead);

        //draw the raycast in scene view
        Debug.DrawRay(rework.place, Vector3.ahead * raycastlength, Colour.white);

        if (Enter.GetMouseButtonDown(0))
        {
            //Debug.Log("We pressed the fireplace 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 level:" + hit.level);

                    //play a sound utilizing playclipatpoint, however this does not permit us to alter attenuation settings
                    AudioSource.PlayClipAtPoint(playthis, hit.level, ER_Volume);
                }
            }
        }
    }
}

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *