Jump to content
  • Advertisement
Sign in to follow this  
Simon James

Unity Use raycast as a trigger

This topic is 600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

I'm trying to trigger an audio file on an object using ray cast. I have a trigger script on the object to trigger the sound which would normally happen by the player walking through it. But can it be done by a raycast trigger. I'm using unity and c#

Share this post


Link to post
Share on other sites
Advertisement
You could write your own raycast trigger
 
using UnityEngine;

public class RaycastTrigger : MonoBehavior {
    public float raycastDistance = 1.0f;
    public LayerMask mask = -1;

    public void Update() {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, raycastDistance, mask)) {
            gameObject.SendMessage("OnTriggerStay", hit.collider);
        } 
    }
}
As a warning that is untested code, it may not even compile but hopefully you can get it to work for you. I didn't implement OnTriggerEnter/OnTriggerExit but you should be able to figure that out. Edited by HappyCoder

Share this post


Link to post
Share on other sites

2nd alternative is good, uses less resources and directly solves the audio rolloff problem, but if you prefer triggers (they're easier to handle in the Editor), do the following:
1- create a trigger volume (a box or sphere collider);
2- add an AudioSource to it, and set the AudioSource Pan field to 0 in order to have a constant sound even if the player walks away;
3- add the script below to the trigger;
4- drag the trigger to the Project window to make it a prefab.

private var inTrigger = false; // becomes true when the player is inside

function OnTriggerEnter(other: Collider){ // check if it's the player (remember to tag it "Player"): if (other.tag == "Player") inTrigger = true; }

function OnTriggerExit(other: Collider){ if (other.tag == "Player") inTrigger = false; }

function Update(){ if (inTrigger && Input.GetKeyDown("e") && !audio.isPlaying){ audio.Play(); } } When you want to add voice to some object, just drag the trigger prefab to it, then adjust the trigger's rotation/position/dimensions and drag the desired sound to its AudioSource Clip field.

Share this post


Link to post
Share on other sites
  gameObject.SendMessage("OnTriggerStay", hit.collider); 

// alternative B Pseudo code:
<Monobehavior class name > tempObj = gameObject.GetComponent<<Monobehavior class name>>();

tempObj.SetTriggerStay(hit.Collider);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!