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