Possible difference between the line on a RayCast and a debug.drawline causing problems.

In the most part, the code is working. But I’m having some issues with the implementation which I’m having some problems wrapping my head around.

The theory is to understand when a ball is on the floor, so I can apply more drag to the item only after the ball is hit, the drag required for when the ball is in the air and rolling on the floor needs to be different. The section of code I’m having issues with is the following, which is applied to the ball:

public float distanceFromFloor;
private bool IsGrounded()
{
Debug.DrawLine(transform.position, transform.position + Vector3.down * distanceFromFloor, Color.white);
return Physics.Raycast(transform.position, transform.position + Vector3.down * distanceFromFloor, groundLayer);

}

The terrain has the layer groundLayer added. The draw line indicates the line shouldn’t leave the ball, however on hitting the ball there’s a frame before the ball leaves the floor that this returns true. Which means the drag while in the air has an incorrect drag applied to it. Also when the ball is returning true when a short distance away from the ball. Which causes the drag to apply too much and causes the ball to freeze in the air:

8405265--1110051--upload_2022-8-31_14-54-45.png

Is this a normal difference between raycast lines and the debug lines? Am I missing something obvious as to why the Raycast is returning true while being quite a considerable distance away from the floor? When it’s above this height, it does return correctly of false.

I’m quite a beginner with coding, but happy to read through documentation if someone has some that pinpoints where I’m wrong.

Yes, you are passing improper parameters to your raycast. Raycast expects a position and a direction. You are giving it two positions. The confusion is exacerbated by your mixing of performing a raycast with a drawLine.

Instead, I recommend the following:

  • If you are using Physics.Raycast, visualize it with Debug.DrawRay
  • If you are using Physics.Linecast, visualize it with Debug.DrawLine

Ultimately your problem here is that you are using transform.position + Vector3.down * distanceFromFloor, as the second parameter for your raycast. This second parameter should just be a direction vector. You should not be including the ray origin in this calculation. Another problem is that you are putting your layer mask into the “max range” parameter.
Be consistent. Try this:

Debug.DrawRay(transform.position, Vector3.down * distanceFromFloor, Color.white);
return Physics.Raycast(transform.position, Vector3.down, distanceFromFloor, groundLayer);

OR:

Debug.DrawLine(transform.position, transform.position + Vector3.down * distanceFromFloor, Color.white);
return Physics.Linecast(transform.position, transform.position + Vector3.down * distanceFromFloor, groundLayer);

Please read:
https://docs.unity3d.com/ScriptReference/Physics.Linecast.html
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

https://docs.unity3d.com/ScriptReference/Debug.DrawLine.html
https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html

2 Likes

Sorry for the long delay - Thanks for highlighting the issue on my side.

  • Debug.DrawRay(transform.position, Vector3.down * distanceFromFloor, Color.white);
  • return Physics.Raycast(transform.position, Vector3.down, distanceFromFloor, groundLayer);

Seemed to be the best way to do this. I had gone down a rabbit hole with drawLine and never thought I was just using the wrong one, even when reading documentation. Also I was using it in the wrong format just because I’d had thought it was the end point as such. Never really understood the direction so that helped me understand that side as well, thank you.

I seem to be getting some issues still with it still returning true while the ball is away from the floor just after the ball is hit, which the debug seems to believe it should be false, but this gives me some more ammo to play about with it and try to get it working.

Many thanks for your time!