- Home /
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);
}else{
isGrounded = false;
animator.applyRootMotion = false;
animator.SetBool("isGrounded", false);
}
I am unclear on what is causing this issue. Any help is appreciated. Thanks!
It's nothing to do with your title. Raycasts are perfectly happy with negative #'s in the start, and/or in the direction. Think about a 3D game with 000 in the middle (common-ish) doing an "in front of me" raycast while facing southWest. Lots of negatives there, and they work.
It's probably something about your setup which is different when z`<`0. $$anonymous$$ight also copy some of those values into Inspector vars (for debugging,) to see if anything got knocked loose.
No this is definitely within the engine itself. I cannot find any script in use that would cause this kind of effect. The whole system right now just seems unreliable and buggy.
There's no need to jump to conclusions when you're experiencing a problem others aren't certain how to reproduce.
Am I correct in assu$$anonymous$$g that colliderOffsety is a constant and 0.2f is to correct it back to being (at least) slightly more than the distance to the ground?
$$anonymous$$ore importantly, I'm getting the impression that if, when your Z value is less than 0, you're no longer triggering collision properly (and, I'm assu$$anonymous$$g, on collision with the same platform as when Z is greater than 0)? If so, it being a problem with the raycasts may be a red herring. That said, it also means that we cannot provide any meaningful feedback or advice without other information relevant to the dilemma.
There is a layered collision system in place on the character, that could be causing some issues. But as far as collision being correct when the z is negative seems to work fine.
So it's only the Raycast itself that's not see$$anonymous$$g to connect (i.e. isGrounded remains false)? Or is there a problem with the CharacterController's behavior otherwise and that only occurs when at a negative Z value?
Sorry for the confusion, but the more detailed information you can offer, the better.
Answer by Lork · Apr 22, 2015 at 02:04 AM
In that case I would investigate along the following lines:
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.
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).
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.
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.
and of course it happens to be number 4, interestingly enough it only occurred when the z position was negative. Thanks Lork!
I took the liberty of converting this comment into an answer.
@$$anonymous$$ajor: If your problem is solved you might want to either mark this answer as correct or close the question.