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
1
Question by sgmongo · Oct 12, 2011 at 01:41 AM · raycastlayermask

Raycast and Layer Masks

Back again, more problems :)

After completing and ironing out all the problems with making a collider based projectile and handling the damage across game objects, I realized as so many do. Fast moving collisions don't work. Kinda wish I was warned, but it lead to some extremely useful knowledge so I'm happy for the experience.

Moving along, I started to learn about ray casting. But I have some questions about it, and how to apply layer masking to it. Heres my code first.

     var hit = (Physics.Raycast (transform.position, target, range));
     if (hit != null) {
          var testhit = hit.GetComponent(test);
               if (testhit != null) {
               print("Companion Cube in sight!");
               }
     }
 

Now as you can plainly see... I haven't gotten far. I was using code who's sintax I cannot seem to fathom using the lovingly emblished reference material. I thought I was applying a layer mask to variable testhit. The layer I was trying to restrict to was test. The only object in my test environment that has the layer test is a cube named 'Companion Cube'. But it was sorta a failure on the debug check. Kept spitting the same mumbo jumbo that every programmer I know hates. Error code: Something~in~greek

So moving along. What I need help with is the sintax involved. If you could show me my trouble spots using the same variables I used. (When people use cookie cutter expamples I get very lost as to what is a function/var/expression/blackmagic etc.)

Also, layer masks seem to be Exclusive only from what I've read (Which really makes me feel my code is fragged) is there a way to write it so that I don't have to isolate the layer mask as a variable and invert it with the whole var = ~var expression?

I'm fairly confident that once I understand the layer mask sintax, so I can restrict the contact of my ray-caster unity will stop having a hissy-cow about unexpected Boolean nonsense.

Next question I'm fairly sure is in-depth. Raycasting appears to cast the ray as far as the distance its marked for. Returning everything it bashes into (Maybe I'm totally wrong there, Sorry if I am please set me straight). But I don't want all my bullets to be piercing. I'd like to be able to restrict the selection to the first, second, third, All, objects it collides with, in order. This way I can add target's pierced. Or all. Or simply stop on the first target. Is there a way to isolate and restrict this or pick it apart as usable information?

On the flip side, if ray-casts don't push beyond the first collision, how am I to accomplish piercing bullets?

I know this is a lot of questions honestly, but they are all the same two subjects in the heading, and I feel splitting all this information up would cause me to lose sight of the concept as a whole. Thanks for reading if you got this far and haven't given up on me!

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 syclamoth · Oct 12, 2011 at 01:49 AM 0
Share

Your second question- there are two kinds of raycasts!

 Physics.Raycast()

only returns the first hit,

 Physics.RaycastAll()

returns an array of everything it hits.

Oh, and as for the layer masks- have a look at this. I have found it endlessly useful!

avatar image sgmongo · Oct 13, 2011 at 07:57 PM 0
Share

Layer$$anonymous$$askExtensions.Create() doesn't appear to exist in my copy of unity. Is this a pro only item, or something I need to download?

I've been at trying to get the java-code syntax to accept it for days in various arrangements. I'm being driven a little bonkers by this. $$anonymous$$y studies have come to a complete halt T.T

That one link looked awesome but sadly I couldn't get it to work, and find its sister-page in java.

avatar image sgmongo · Oct 13, 2011 at 08:08 PM 0
Share

And what exactly does >> mean. Its in the 'layermask' portion of the script reference. They show the example calling it a bitshift which means nothing to me sadly (Even after trying to figure it out via the gooles). Why would they use an example like that? I'm sorry to fume but if they'd used something common, I might have figured it out by now.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by srmojuze · Apr 04, 2014 at 11:18 AM

Try this: http://docs.unity3d.com/Documentation/Components/Layers.html ...Basically each layer has a number (integer). Then you use the "bitshift" to specify which layers you WANT to collide the ray with and which you DON'T. Good luck!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Detecting that I'm clicking a unit even though I'm not? 0 Answers

Four different Raycast functions? 1 Answer

Raycast layer mask problem 1 Answer

My raycast ignores layer mask, could you give me some advice? 1 Answer

Make mask layer have priority over other mask layer on raycast 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