Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Peanut97 · Jul 26, 2013 at 09:56 PM · raycastaudioraycastinghit

Help Playing "Sound Clip" on RayCastHit? (RayCast help?)

Hello,

I am currently learning how RayCasting works and I need some help/example scripts. (I use Java.) Basically, I am using this script below to create a "RayCast" in front of my player. I am trying to make a script that plays an audio clip (one shot) when this "RayCast" hits a gameObject called "Monster." If anybody can help me out with this... I would appreciate it A TON! Thanks so much for your help!

 function Update () {
         var fwd = transform.TransformDirection (Vector3.forward);
         if (Physics.Raycast (transform.position, fwd, 100)) {
             print ("There is something in front of the object!");
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gregzo · Jul 26, 2013 at 10:10 PM

Hi!

If you look a little further down in the docs concerning raycast, you'll find that you can pass a RaycastHit object as parameter to the raycast. Here's your script, adapted :

 function Update () {
     var fwd = transform.TransformDirection (Vector3.forward);
     var hitInfo : RaycastHit;
     if (Physics.Raycast (transform.position, fwd, hitInfo, 100)) {
     print ("There is something in front of the object!");
     print ("that something's name is "+hitInfo.transform.name );
     }
 }

Check out the RaycastHit struct in the docs.

Playing a oneshot clip should be fine after that ( audio.PlayOneShot ( clip ); ).

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peanut97 · Jul 26, 2013 at 10:15 PM 0
Share

So.. Basically... You're saying this script will play a One Shot Audio Clip when hit by the RayCast? Are you sure that's right?

 function Update () {
     var fwd = transform.TransformDirection (Vector3.forward);
     var hitInfo : RaycastHit;
     if (Physics.Raycast (transform.position, fwd, hitInfo, 100)) {
     print ("There is something in front of the object!");
     print ("that something's name is "+hitInfo.transform.name );
      ( audio.PlayOneShot ( clip ); 
     }
 }
 
avatar image gregzo · Jul 26, 2013 at 10:21 PM 0
Share

Not quite... In 3 steps : 1) You raycast and check if the ray hit 2) If it has hit, you check what it has hit 3) If it's a monster, play the sound

How you check if it's a monster is up to you : you could have a monster layer, or a List of all curent monsters, or you could use GetComponent to see if the GameObject that has been hit has a script of type $$anonymous$$onster attached to it, etc...

avatar image Peanut97 · Jul 26, 2013 at 10:22 PM 0
Share

Could you modify this C# script to play a sound clip ins$$anonymous$$d of destroying the object "$$anonymous$$onster?"

 using UnityEngine;
 using System.Collections;
  
 public class Example : $$anonymous$$onoBehaviour
 {
     RaycastHit hit;
  
     void Update()
     {
         if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, $$anonymous$$athf.Infinity))
         {
             if(hit.collider.gameObject.name == "$$anonymous$$onster")
             {
                 Destroy(hit.collider.gameObject, 1);
             }
         }
     }
 }
 
avatar image Peanut97 · Jul 26, 2013 at 10:25 PM 0
Share

That script works... I've tested it... If I could just change "destroy" to "play sound"... That would solve my problem...

avatar image gregzo · Jul 26, 2013 at 10:27 PM 0
Share

Just change the Destroy line for audio.PlayOneShot ( myClip, myVolume ); myClip needs to be declare next to RaycastHit :

public AudioClip myClip; and assigned in the inspector. I really wouldn't advise checking against names, though...

Show more comments
avatar image
0

Answer by HanSoloYolo · Jul 23, 2017 at 08:26 PM

Guys I am really sorry to bother you on this old post, but I still have the same question without a clear answer. How would you modify this Destroy on RaycastHit script to Play an Audio file attached to the object you are hitting with Raycast?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TouchDestroyer : MonoBehaviour 
 {
     Ray ray;
     RaycastHit hit;
 
     // Use this for initialization
     void Start () 
     {
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (Input.touchCount > 0)
         {
             ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
             if (Physics.Raycast (ray, out hit, Mathf.Infinity))
 
             Destroy (hit.transform.gameObject);
         }
     }
 }

To clarify, I want to be able to touch an object like a dog and have it bark, then touch a cat and have it meow without destroying it.

However, I would like to learn how to play a sound attached to an object AND THEN destroy it with a similar script.

Seeing both of these options will help me learn a lot about scripting and touch controls. I have spent over a week now trying to figure this out.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can anyone help me modify this script? (C#) 2 Answers

Getting a point on ray 1 Answer

How to use raycast on generated objects. 0 Answers

Vehicle is vibrating on the edge of the ramp/surface when I get the normal of the surface 0 Answers

how to select an object with raycasting 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges