• 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 Zarinel · Mar 02, 2019 at 08:26 PM · raycastraycasthitrayraycasthit2draycasts

RayCast2d Not colliding with objects in other layers

I have the following code. My player and certain objects are in PlayerAndEnemies. I have a tilemap2d (which is what I want the raycast to hit) but it does not collide with it. The tilemap 2d does have a tilemap collider and is in other layers (such as default, and others I created). Can anyone point me in the direction for why this would not collide?

 Movement movement = player.GetComponent<Movement>();
                 RaycastHit2D hit;
                 int layerMask = LayerMask.GetMask("PlayerAndEnemies");
                 //layerMask = ~layerMask;
 
                 if (movement.directionFacing == 0)
                 {
                     hit = Physics2D.Raycast(player.transform.position, Vector2.down, 100f, layerMask);
                 } else if (movement.directionFacing == 1)
                 {
                     hit = Physics2D.Raycast(player.transform.position, Vector2.up, 100f, layerMask);
                 } else if (movement.directionFacing == 2)
             {
                 hit = Physics2D.Raycast(player.transform.position, Vector2.left, 100f, layerMask);
             } else // 3
             {
                 hit = Physics2D.Raycast(player.transform.position, Vector2.right, 100f, layerMask);
             }
             //Debug.DrawRay(hit, Color.red);
             //Check distance of raycast
             print(hit.distance);
             print(hit.collider);






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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WarmedxMints · Mar 02, 2019 at 09:05 PM

A raycast looks for gameobecjts. If you have specified a layermask, then it will only look for objects selected in that mask. A gameobject can only be in one layer. You would need to put the gameobject with the tilemap contains the tiles you wish to raycast against on a layer which is in your desired layer.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Raycast returns null for no apparent reason 0 Answers

set ray.point as x, y and z float 1 Answer

How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider. 2 Answers

RaycastHit.transform/collider 1 Answer

"Operator `??' cannot be applied to operands of type `bool' and `bool'" Error with C# 6's "??" operator 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges