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 Setzer22 · Jun 06, 2012 at 08:38 PM · raycasthit

rayCastHit question

Hello everyone

So I'm trying to make a public class which does some calculations which i'll be using a lot, i've called that class HandyMethods.cs.

What I wanted to do, is to make a method which checks if there's a collision between A and B (with the Physics.Raycast() function), and if there is, return a rayCastHit variable which I could then use in many other scripts. But what I want, is to have an easy way to check if this function didn't find a collision (the Physics.Raycast returned the "false" value).

If I define my function like this

 public RayCastHit CheckForCollisions(){}

it would only be able to return a RayCastHit type variable, right?

So how do I make the function so it can return both a "true" boolean value and a RayCastHit type variable?

Comment
Add comment · Show 3
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 kolban · Jun 06, 2012 at 08:57 PM 2
Share

I would suggest creating a custom Class that contains two properties. One an instance of a RayCastHit and the other a boolean. Your function would then return an instance of this class which you can then check.

Another solution, would be return "null" for the RayCastHit to represent the fact that no hit happened.

avatar image Fattie · Jun 06, 2012 at 09:42 PM 0
Share

Handy$$anonymous$$ethods.cs is a great name!

avatar image Setzer22 · Jun 07, 2012 at 05:15 PM 0
Share

Thanks that explained it ^^

And yes @Fattie, it's a great name :D Everyone should have one of those

1 Reply

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

Answer by rutter · Jun 06, 2012 at 09:40 PM

Two ways come to mind:

  • Return null if there was not hit, and check for that condition when calling the function.

  • Return a bool indicating whether or not there was a hit, and use an out parameter to store the RaycastHit, if any.

Unity's own raycast functions opt for the latter option. You can see some examples here: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

If you find the out keyword confusing, it may be a good idea to read up on searches like "csharp out keyword" or "pass by reference".

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

6 People are following this question.

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

Related Questions

Ray from enemy to player is always a bit off (Image) 1 Answer

RaycastHit.distance 1 Answer

Hit distance Change light range 1 Answer

How to hit two objects with only one shot (raycast) 0 Answers

Need help with Third person shooter 0 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