Why isn't my raycast and debug.drawline working properly?
As shown in this video, the raycast to check if my player is grounded is not working right. It prints that I am grounded at the very start of runtime, and then never prints it again, essentially allowing infinite jump. The Debug.DrawLine isn't helpful in debugging either because it seemingly only points to origin of the scene usually, even when using hit.point for the direction, which admittedly doesn't make sense but Vector3.down does pretty much the same thing excluding point below the player when grounded. I don't know if that's what the raycast is doing or not, but I assume it is because it isn't working properly. This isn't worded very well, but if anyone knows how to help, that would be really appreciated :).
Here's the relevant code, also Jump() is called constantly in Update() as shown in the video, might that be the source of the problem by any chance?
void Jump() {
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, jumpRaycastLength)) {
if (hit.transform != null)
{
isGrounded = true;
}
else {
isGrounded = false;
}
}
if (Input.GetButtonDown("Jump") && isGrounded) {
rb.AddForce(0, jumpForce, 0);
}
Debug.Log(isGrounded);
Debug.DrawLine(transform.position, hit.point, Color.red);
}
Your answer

Follow this Question
Related Questions
How to change the position of a Raycast (on Center of the Player 2D)? 0 Answers
Raycast hit.point differs from Ray direction [SOLVED] 1 Answer
How do I add ground tolerance and ledge grabbing to Sebastian Lague's Unity 5 2D Controller? 0 Answers
Still constant jumping even with raycasting. 0 Answers
Simple Jumping Problem with raycast,Simple Jumping Problem 1 Answer