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 jacknorrisuk · Sep 13, 2014 at 04:14 AM · animationraycastaudiotriggerraycasthit

Trying to play other objects' sound and animations from Raycast hit

I just have a raycast coming from the camera, which I want to use to trigger a sound, and start animation playback, on multiple other objects. It's pretty much a 'start' button for the whole scene - activated just by looking.

I don't know what the best way is to simply send the 'play' command to other objects, as I have read about performance hits when searching for objects' name every frame.

Here's the example raycast I'm using at the moment.

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
         // Get the ray going through the center of the screen
         var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
         // Do a raycast
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit))
             print ("I'm looking at " + hit.transform.name);
         else
             print ("I'm looking at nothing!");
     }

Would really appreciate some guidance and example code for this issue, many thanks in advance!

Comment
Add comment · Show 1
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 vexe · Sep 13, 2014 at 07:15 AM 0
Share

It depends on your design. Do the other objects have AudioSources and Animation components on them? how do you intend to play the animation? is it legacy or mecanim? - I would just do:

 if (the ray hit something)
 {
    var go = hit.collider.gameObject;
    var audio = go.GetComponent<AudioSource>();
    if (audio) audio.Play();
    var anim = go.GetComponent<Animation>();
    if (anim) anim.Play();
 }

This will change according to your design - like, you could have only one main audio source that you want to play when you hit those objects, etc.

Give us more info and we'll give you more help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jjplay175 · Sep 13, 2014 at 07:02 AM

Not 100% sure but I think I have an idea of how you can achieve the play sound, would it be possible to have a private Gameobject Var and set what ever the raycast is looking at as the variable and then have the same script on every object and just play that, I'm not sure how that would go for performance though

I only code in c# so you might have to fix any mistakes

 private var Active : Gameobject;
 
 function Update () {
         // Get the ray going through the center of the screen
         var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
         // Do a raycast
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit))
             if (Active == hit)//If object is the target return
             {
                 continue;}//Continue looking for new target
             else
             {
             Active = hit;//Set viewed object as target
             var SelSong = Active.Getcomponent<SongScript>();
             SelSong.enabled = true;
             print ("I'm looking at " + hit.transform.name);
             }
         else
             print ("I'm looking at nothing!");
     }

I hope that helps, it might need a few changes but the general idea should work, hope it helps, I'm quite new to this myself ^^

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

stop triggers from retriggering animation 2 Answers

Raycasting Door Animator 1 Answer

Continuous RayCast on GetButtonDown 0 Answers

C# - Trigger Animation with Raycasting? 1 Answer

Raycast to trigger audio from other script on hit? 2 Answers


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