What is my raycast doing here?? (C#)

Hi,
I got a problem with my raycast. It’s kind of snapping to the worlds origin Vector3(0,0,0) and I dont know why.
Uploaded a video because I suck at describing things. - YouTube
My c# code:

RaycastHit hit;
		if(Physics.Raycast (transform.position, -transform.up, out hit, rayDis)){
			Debug.Log("There is something in front of the object!");

		}
		Debug.DrawLine (transform.position + new Vector3(0,1,0), hit.point, Color.cyan);

Since the hit variable is “filled” by the Raycast, hit.point will only have a value assigned if the raycast hit something.

This means that if your raycast isn’t hitting something, the hit.point value will be the default value for a Vector3, which is (0,0,0).

You’re adding (0, 1, 0) to your position in the DrawLine, and that’s what’s tricking you. The center of your… tank? is at the bottom of the model. This means that when it’s standing on the ground, the raycast’s origin is inside the ground, and you won’t hit anything.

Solve this by either adding (0, 1, 0) to where you’re raycasting from, or by adjusting your model and collider so the object’s centre is in the middle of the model and collider, instead of at their bottom.

Good luck! Have fun making games!