Godot Version
godot 4.6.1
Question
I’m trying to measure the distance to the floor from the bottom of a CharacterBody2D/CollisionShape2D to change the frame of the jump animation depending on the distance to do the landing animation when it gets close to the ground, but I can’t figure it out.
You may need to use a raycast, a RayCast2D can give you the nearest collision point, from there you can use Vector2’s distance_to
2 Likes
I’m trying, but its saying Error: Function “distance_to()“ not found in base self.
Sorry if I’m being dumb, I’m new to gdscript.
distance_to() is a function found in Vector2, you need a Vector2 variable to use it.
To use it properly you need two Vector2’s serving as points to calculate the distance, so, for example, you can use the player’s position and grab the position to the floor point.
Here’s an example:
var point_one: Vector2 = Vector2(0,0)
var point_two: Vector2 = Vector2(0,1)
print(point_one.distance_to(point_two))
The function results a float which is the distance between the two Vector2’s, in this case 1.0.
You can apply this logic inside your player script to measure the distance between the player’s position and the floor by using a raycast like so (this is just an example, you’ll need to adapt it to your game):
var raycast: RayCast2D
var raycast_hit_position: Vector2
var distance_to_floor: float
raycast_hit_position = raycast.get_collision_point()
distance_to_floor = raycast_hit_position.distance_to(self.global_position)
1 Like
Thank you!!! This worked perfectly!
1 Like