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 Haigotron · Dec 02, 2010 at 04:18 PM · raycastcollidertriggerraycasthitray

Is there a way to detect if an object is being hit by a raycast?

Hello,

I tried looking for another thread, but I couldn't find what I was looking for. Basically, I wanted to know if there is a way for an object to detect if it is being hit by a ray.

I assume using a trigger or collider should work, correct? Do rays collide with colliders or triggers? Can I use them to determine if my object is being hit by a ray?

Thanks alot 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 Haigotron · Dec 02, 2010 at 04:19 PM 0
Share

I could test it myself, but I am not at home right now :)

2 Replies

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

Answer by skovacs1 · Dec 02, 2010 at 04:52 PM

If I read correctly, you're asking if there is a way for an object to know if it has been hit by a raycast, correct?

The short answer is no, because it's rather pointless to inform the object that we hit it with a test, but you could always do that on your own where you do the raycast with something like:

Wherever you raycast:

var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit)) {
    hit.transform.SendMessage("HitByRaycast",gameObject,
                              SendMessageOptions.DontRequireReceiver);
}

Whatever cares if it was hit by a raycast:

function HitByRaycast(source : GameObject) {
    SayOuchTo(source);
}

You may as well just call the function in the script that cast the ray rather than splitting the code across multiple files and adding the overhead of sending the message when you don't need to.

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 LonePawn · Apr 14, 2015 at 07:46 AM 0
Share

Excellent answer by skovacs. However I think, there are situations where sending the message to the hit object can be very useful. In fact, that's why I came searching for your answer. Imagine if your game object doesn't want to say 'ouch' immediately it gets hit. But would like to keep a record of the hitter so it can come for revenge at a later time. This is just an example but I do think it can be very useful. Thanks for answer btw.

avatar image
0

Answer by Krobill · Dec 02, 2010 at 04:35 PM

you have to add a Collider Component to your GameObject to be able to make a raycast on it. If you want 'perfect' collision detection you'll have to use a MeshCollider. Depending on your mesh complexity and the number of collider you intend to use, it might not be a good idea.

You can raycast against a specific object/collider or raycast against your entire scene and see which object/collider hits first.

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

No one has followed this question yet.

Related Questions

Make raycast ignore some triggers but hit some others? 2 Answers

Box Collider wont trigger a Trigger after moveing the object 0 Answers

Need Help with RayCast. No Vector? 1 Answer

RaycastHit.transform/collider 1 Answer

Physics.Raycast not hitting anything 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