Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 jtsmith1287 · Feb 11, 2015 at 05:37 AM · c#physicsraycast

Physics.Raycasy not really using Layermask?

I've got the following statement:

         bool rayHit = Physics.Raycast(
                 transform.position, // position of the player
                 Turret.forward, // the direction the player is aiming
                 out hitInfo,
                 Transform.FindObjectOfType<SceneHandler>().TargetingLayerMask); // set to Player and EnemyShip physics layers

I have the TargetingLayerMask set to include ONLY the Player layer, and the EnemyShip layer. I also have a PlayerProjectile layer and an EnemyProjectile layer, both of which are not checked (I set the layers for the mask in the inspector). Despite this, rayhit is true when the ray hits any of the projectiles. I've already verified all the objects are allocated to the correct layers. Rayhit needs to only be true if the player is targeting an enemy or ally. Currently it appears to be true if it hits anything.

Comment
Add comment · Show 2
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 NoseKills · Feb 11, 2015 at 07:03 AM 0
Share

What type is ScenaHandler.TargetingLayer$$anonymous$$ask ? Int or Layer$$anonymous$$ask

avatar image meat5000 ♦ · Feb 11, 2015 at 05:41 PM 0
Share

Layer$$anonymous$$ask is int.

But you must realise that its not 1-32 to select each layer as you might think.

An int is made of 32 bits and each bit represents one of the 32 layers. The int value you enter must represent the bit value 1 of the binary word.

alt text

In the diagram there are 32 bits (0s or 1s). The numbers lined to it represent the decimal values of that bit if that bit were to be a 1. The value doubles with each digit to the left.

So layer 4&8 would be 8+128 = 136 Decimal.

binarydigitvalues.jpg (26.7 kB)

1 Reply

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

Answer by Baste · Feb 11, 2015 at 08:16 AM

Physics.Raycast looks like this:

 public static bool Raycast(
     Vector3 origin, 
     Vector3 direction, 
     RaycastHit hitInfo, 
     float distance = Mathf.Infinity, 
     int layerMask = DefaultRaycastLayers);

which means that you're sending in your layer mask as the distance parameter. Since the layermask is an integer (wrapped up in an enum), that's cast to float, and you don't get any compilation errors.

To get the behavior you want, just send in Mathf.Infinity (or float.PositiveInfinity) as the fourth argument, and your layermask as the fifth.

Comment
Add comment · Show 2 · 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 jtsmith1287 · Feb 11, 2015 at 05:08 PM 0
Share

Does the position of the out parameter not matter? Your example doesn't match any of the api reference examples Neither does $$anonymous$$e but I'll give your example a shot.

avatar image Baste · Feb 11, 2015 at 07:29 PM 0
Share

The position matters. That's not an example I've written, that's the actual declaration of the method as it's in the source.

The distance and layermask parameters has the distance and layermask parameters as optional arguments. This means that if you don't send in the parameters, the default values $$anonymous$$athf.Infinity and DefaultRaycastLayers will be used ins$$anonymous$$d.

in your case, you're sending in your layermask in the position of the distance parameter. You're not sending in anything in the position of the layermask, so the default layermask (hits everything except raycast ignore) will be used.

And you should probably read up on optional arguments, here. They're quite useful. Note that named parameters is something else, but it's in the same article.

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

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

Related Questions

What is wrong about this RaycastHit Event??? 1 Answer

Can't Make Raycast Target Nearest Character 1 Answer

Extending Survival Shooter Tutorial Using RaycastAll to attack all enemies in a line. 1 Answer

Distribute terrain in zones 3 Answers

Raycast doesn't work properly? 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