- Home /
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
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.
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...
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().
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)
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
![](https://koobas.hobune.stream/wayback/20220613124238im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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