While developing a game, I wanted to enable characters to “see” where they’re going; that is, to tell them what they’re about to stumble into, without using colliders. The answer was to cast a Ray, angled slightly downward, in front of the character, so he can “see” if he’s about to walk into a tree, or a river, before it happens.
The image at the top of the post shows a character (dog) casting a Ray (yellow line) downward in front of him, through the floor.
But the Ray intersects a tree and points the object out to him (red line).
I do this like so:
// cast the ray from slightly ahead of the character
Vector3 relative = transform.position + transform.forward;
// look forward and down slightly
Vector3 direction = new Vector3(transform.forward.x, -0.3f, transform.forward.z);
// draw a debug line for testing purposes only
Debug.DrawLine(relative, relative + (direction * 15.0f), Color.yellow, 0.05f);
// declare the ray's hit info
// cast a ray 15 units into space and see if we hit anything
if (Physics.Raycast(relative, direction, out hit, 15.0f))
// do stuff here, just as slow the character
Debug.DrawLine(relative, hit.collider.transform.position,Color.red, 0.1f);