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 ZingFreelancer · Apr 09, 2012 at 08:58 AM · collisionraycasttriggersinteraction

How can a game object detect that it is being hit by a raycast?

When character controller is within 3 meters of a game object (lets say a cube) he has the option to press "space" to interact with said cube regardless if character controller is facing the cube or not. I've done that with a small trigger zone around intractable cubes and the trigger zone ignores raycast.

But I need interaction to function only when character controller is within 3 meters of the cube (inside cubes trigger zone) and are casting ray on/looking at the cube.

I tried adding function OnCollisionEnter(collision : Collision) or onCollisionStay to the object, but it does not seem to be triggable by raycast...

I kinda wish to avoid coding a complicated raycast if-else script on the character controller, especially when I have many intractable objects on the scene.

Any suggestions?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kleptomaniac · Apr 09, 2012 at 09:35 AM

Use Vector3.Distance instead of a raycast. So attach something like this (pseudo-code) to your character controller :

 var cube : Transform;
 
 if (Vector3.Distance(transform.position, cube.position) <= 3) {
     if (Input.GetKeyDown(KeyCode.Space)) {
         //Do whatever you want to do here
     }
 }    

Hope that helps, Klep

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

Answer by ZingFreelancer · Apr 09, 2012 at 10:37 AM

Thanks for the reply Kelp, this is certainly an interesting proposition. However, I need raycast, when character controller is looking at an object I display that's object name on screen.

But what I really want to know, is there a way for the character controller to tell the object of interest to tell when it is being hit by raycast?

There is a way to tell an object when something collides with it, if only I had a function like "OnRaycastStay" or "OnRayCastHit", or a different function that could detect when ray collides with an object, similar to how OnControllerColliderHit or OnTriggerStay behave.

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

Answer by FizzyBear · Apr 09, 2012 at 10:28 AM

For mine I used;

  if (Physics.Raycast (ray, hit, range))
     {    
         // we hit something      
         print ("I'm looking at " + hit.transform.name);
         Debug.DrawLine (ray.origin, hit.point);
     }

This one allows you to detect whether it's hitting anyting.

But if that's not what you want, then Kleps answer is perfect. :)

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 Kleptomaniac · Apr 09, 2012 at 10:55 AM 0
Share

$$anonymous$$ake sure to declare your ray, hit and range variables @FizzyBear.

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

6 People are following this question.

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

Related Questions

Unreliability of Physics.Raycast.Distance for player movement? 0 Answers

How To: Spawn object on raycast collision. 2 Answers

Destroying all objects with a certain tag that ray cast if colliding with 0 Answers

Receiving information about getting hit by a Raycast 2 Answers

Surface with hole and Raycast - Which collider 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