Raycast doesn't work. Why?

Hi.

I’m trying to make a simple plataform game, but I couldn’t check if the player is grounded or not… Anyway, here’s one piece of my code:

		Ray rayY = new Ray (playerPosition.y, colliderY);
		Debug.DrawRay (rayY.origin, rayY.direction, Color.white);

		if (Physics.Raycast (rayY, 1f, colliderMask)) {
				grounded = true;
				gravity = 0f;				
		} 
		else {
			gravity = -6f;
			grounded = false;
		}

		playerPosition.y += gravity * Time.deltaTime;

		move ();

		}

When I run the code, my char keeps falling down for ever, ignoring the collision with the ground.

PS: Yes, I’ve set the ground object on the correct layer:

26755-attach01.png

PS II: With the “Debug.DrawRay” I can see the code casting the ray in the right direction:

26756-grounded.png

What am I doing wrong?

Thanks :slight_smile:

Thanks @bunnybomb7670, your suggestion works fine.