3D Raycasting: Percentage length of raycast before it interacts with a surface

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HLutece

The idea is to create a function that calculates the percentage of a raycast’s length before it collides with a surface and prints the result. This will need to be updated every physics frame.

I’ve looked into tutorials covering similar effects but since this is my first attempt at learning scripting I am still confused on how to approach this. Any help is greatly appreciated.

:bust_in_silhouette: Reply From: zhyrin

Suppose you have your Raycast3D in a variable called raycast, once you detect a hit:

# it's in global coordinate system
var collision_point: Vector3 = raycast.get_collision_point()
# value between 0.0 and 1.0
var percentage: float = 0.5
var percentage_length: float = raycast.global_position.distance_to(collision_point) * percentage