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 byte89 · Dec 15, 2013 at 10:14 PM · 2draycasthitphysics2d.raycast

2D Raycast doesn't Hit colliders

Hello everybody, i have a problem with a RaycastHit check, for some reason it doesn't detect any hit on colliders. This is my code:

 void FixedUpdate ()
     {
         velocity = player.GetComponent<PlayerMotion> ().velocity;
         deltaTime = player.GetComponent<PlayerMotion> ().deltaTime;
         var ray = new Vector2(target.position.x , target.position.y) + new Vector2(0.4f,0f);
 
                 if (velocity.x < 0) {
                         rayDirection = -Vector2.right;
                         rayDistance = velocity.x;
                 } else if (velocity.x > 0) {
                         rayDirection = Vector2.right;
                         rayDistance = velocity.x;
                 } else if (velocity.y < 0) {
                         rayDirection = -Vector2.up;
                         rayDistance = velocity.y;
                 } else if (velocity.y > 0) {
                         rayDirection = Vector2.up;
                         rayDistance = velocity.y;
                 }
 
 
         rayDistance = Mathf.Abs (rayDistance * deltaTime);
 
         Debug.DrawRay (ray,rayDirection, Color.green);
 
         var _raycastHit = Physics2D.Raycast(ray , rayDirection, rayDistance, LayerMask.NameToLayer("TestLayer"));
 
 
         if ( _raycastHit )
                         Debug.Log ("Hit!");

Using TestLayer cause these are the only objects i want the collision to be detected with. Thanks to debug.DrawRay() i can clearly see my Ray hitting on the desired collider, but the debug log "Hit!" never shows up so i assume the raycastHit is not being detected. What am i missing?? (velocity is the player movement taken from another class) Thanks a lot

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
Best Answer

Answer by robertbu · Dec 15, 2013 at 10:18 PM

This is most likely your problem:

 rayDistance = Mathf.Abs (rayDistance * deltaTime);

If your game is running at 60 fps, the ray you are using in your Debug.DrawRay() is approximately 60 times longer than the one you are using in your Physics2D.Raycast(). I'm not sure why you feel you must scale the distance by deltaTime, so it is hard to give you a definitive fix.

Comment
Add comment · Show 4 · 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 byte89 · Dec 15, 2013 at 11:00 PM 0
Share

I'm using deltaTime cause i'm calculating the player movement (this script is a camera script that moves along with the player but has to handle some particular situations), since i use something similar for the real player movement. This way the ray should check if the future position is colliding.

I've tried to remove the deltaTime anyway just to check if the collision would be detected this way. but it doesn't work :/

I'm not sure if the layer$$anonymous$$ask is working properly: The object that has this script attached to is on layer that shouldn't collide with the objectCollider layer (as setted on Physics2DSettings), but even if i enable the two layers to see eachother on the layer collision matrix, still the collision is not detected.

Anyway i need to normally disable the detection between these 2 layers, except when i explicitly ask for it with the layer mask on this script...

avatar image robertbu · Dec 15, 2013 at 11:18 PM 0
Share

From just reading the spec, your layer mask calculation has an issue. 'Layer$$anonymous$$ask.NameToLayer("TestLayer")' returns the index of the layer. It does not return the value to be used in a mask. There should be a clean way to do this, but I don't see it. The easiest solution would be to declare at the top of the class:

 public Layer$$anonymous$$ask lm; 

Then in the inspector you can pick your layer and use 'lm' in your Raycast2D().

As an alternate, you can do this just before the Raycast2D():

 Layer$$anonymous$$ask lm = 1 << Layer$$anonymous$$ask.NameToLayer("TestLayer");

Then use 'lm' in the Raycast2D().

avatar image byte89 · Dec 15, 2013 at 11:44 PM 0
Share

Thanks, using this method works! so it needs to have a binary value even if it's not specified in the raycast function (i thought it just needed the layer index since it said a int value was needed)

avatar image robertbu · Dec 16, 2013 at 12:09 AM 0
Share

A layer mask is a set of flags. Each layer is encoded in one bit. That way a mask can specify more than one layer. It is still an integer, but it's not the index of a single layer.

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

16 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

Related Questions

Bitmask/LayerMask used in raycast not working 2 Answers

Physics2D Raycast not detecting wall colliders 1 Answer

2D Raycasting to check if grounded not working 1 Answer

Yet another NullReferenceException question. 2 Answers

help with multiple sounds 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