Negative Positions Breaks Raycasts

For some reasons when I move my player controller so that its position is negative on the z axis the script controlling it just stops working, as soon as I move it back to positive it’s like nothing ever happened. It seems that the problem is isolated in the grounding function which uses raycasts. The code looks like this:

RaycastHit hit;
			if(Physics.Raycast(transform.position, new Vector3(0, -GetComponent<Collider>().bounds.extents.y, 0), out hit, GetComponent<Collider>().bounds.extents.y - colliderOffsety + 0.2f)){
				isGrounded = true;
				animator.applyRootMotion = true;
				animator.SetBool("isGrounded", true);
				isGrounded = false;
				animator.applyRootMotion = false;
				animator.SetBool("isGrounded", false);

I am unclear on what is causing this issue. Any help is appreciated. Thanks!

In that case I would investigate along the following lines:

  1. In the negative Z, are you moving onto a different gameobject representing the ground? If so, double check to make sure it’s not on the “Ignore Raycasts” layer.
  2. What you have for the direction parameter of the raycast shouldn’t cause any issues (keyword shouldn’t), but for simplicity and efficiency’s sake, you should really be using a unit vector indicating the desired direction (eg. Vector3.Up * -1).
  3. Is colliderOffsety a constant or is it changed by something? If it becomes too high, the raycast may not be able to reach the ground.
  4. What position on the character does transform.position actually represent? Typically it’ll either be the center or the bottom. If it’s the bottom then the raycast is probably poking through the ground half the time. If that’s the case you’ll need to move the starting position up a little bit and reconsider your distance calculations.