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 $$anonymous$$ · May 18, 2014 at 11:41 AM · gameobjectraycastraycastingraycasthit

Ray Casting - Trigger function

Hey guys, simple question about ray casting:

Does Unity provides a function similar to OnTriggerEnter() that is automatically fired every time an object was hit by a Raycast?

Let me clarify the problem:

I have two scripts. The first one is attached to the PlayerController camera (which fires a Raycast on the Update method), the second one called KeyObjectController is attached to every GameObject in the scene that is marked as a key object. Now, from this script, I'd like to create a Lerp animation on the material to create a sort of highlight effect, only when the object is hit by the player camera Raycast. I know I could check the hit game object tag directly form the player script, and call the sendMessage method to invoke the Highlight function, but I'd like to keep the logic directly into the KeyObjectController script.

Is that possible?

Thank you guys.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · May 18, 2014 at 02:16 PM

Unity does not provide this functionality. You are going to somehow communicate to the object from the Raycast() code. As an alternate, you can create a public function and call it directly. So say in your KeyObjectController script you had a RaycastHitMe() function. From the raycast code you could do:

  var key = hit.collider.GetComponent(KeyObjectController);
  if (key != null)
       key.RaycastHitMe();

If you are using C#, another possibility is to use Events and Delegates and have all of your KyeOjbectController scripts listen for a Raycast hit event, and then check the transform hit against its own transform.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · May 18, 2014 at 03:19 PM 0
Share

It's a pity Unity lack of this feature, but your solution works great so it's not a big deal. Since we don't also have the counterpart "onRayCastExit" function, I have to save a reference to the lastest game object that was hit by RayCast, and call the "key.RayCastExit()" function when a new object was hit. Again, thanks for your help Robert.

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

19 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 avatar image avatar image avatar image

Related Questions

My raycast is ignoring my tilemap collider 0 Answers

Problem With Raycasthit Angle 0 Answers

Store previous gameObject that raycast had hit. (Or store any previous value in Update for that matter) 1 Answer

Raycast returns null for no apparent reason 0 Answers

Raycast not working 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