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 4u2fap · Sep 17, 2013 at 07:43 PM · gameobjectraycastdistance check

detecting an object distance behind an other one with a raycast

hi, i was wondering if there is a way to detect the distance between an object i am looking at and the other object behind it with a raycast? for example i whant to look a a crate and when i press lets say "E" it check if the object is let say 5 unit or closer to the crate and if it is, it then do someting and all that using raycast. you must keep in mind that they will be more then one crate.

Comment
Add comment · Show 4
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 fafase · Sep 17, 2013 at 07:53 PM 0
Share

Do you need to raycast the first object? If not use a layer on it so that it gets ignored.

avatar image 4u2fap · Sep 18, 2013 at 01:08 AM 0
Share

I need the raycast to hit the first object and then detect the distance between that point it hit and the object behind it

avatar image instruct9r · Sep 18, 2013 at 01:32 AM 0
Share

What you can do is this.

  • 1: Raycast to the first object and store it's distance

  • 2: Add the second object to a Layer

  • 3: Raycast again with layer$$anonymous$$ask, so
    you can ignore everything except the Layer, that the second object is and store it's distance.

  • 4: subtract the distance of the first object, from the second's distance...

avatar image robertbu · Sep 18, 2013 at 02:11 AM 1
Share

You can use Physics.RaycastAll() to get multiple hits. The hits will not necessarily be sorted by distance.

1 Reply

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

Answer by kumarc123 · Sep 18, 2013 at 07:12 AM

You can easily do it

  1. Use Physics.Raycast (other.position, Vector3.back, hit).

  2. Check if hit.gameObject ot transform is the desired one or not. Ex: if (hit.gameObject.tag == "target")

  3. get the distance from hit.distance property

  4. Thats it

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

17 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

Related Questions

See what object I hit in raycast 0 Answers

Camera switch between child back to main camera issue 2 Answers

How do I ask if RaycastHit returns null? 3 Answers

Class hierarchy, gameObject, Raycast 1 Answer

Instantiated Objects not being set at ground/terrain level?(Solved) 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