I'm trying to find a way to determine the "global" angle of the floor surface the player just collided with: That is, I don't care if I collided head on with a wall at this step, I just want to know the angle of the collision surface relative to the x axis (or y axis, whichever is easier to code for, really). I've tried MULTIPLE methods of acheiving this... and they all seem to give inconsistent results. Here's the code I've been using for just a basic test of the principle, with Characterbody2D, default settings, a hitbox, a Camera2D, and the following script:
`extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var ground_vel = 0
var ground_accel = 300
var ground_angle = 0
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
#else: velocity.y += 0.5gravitydelta
# Handle Jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
ground_angle = get_floor_angle()
print(get_floor_angle())
`
This scene is brought in by my dummy level as a child node, and is the ONLY scene to have any kind of script, so this is the only script that should be active. The collision here consists of basic collisionshape2D's rotated and sized as desired. What's important here is that the main collision I'm testing is against a long rectangular piece rotated to 26.2 degrees incline (assuming rightwards motion).
The problem is that I don't get consistent values printed along this flat surface: not only do the results vary depending on speed and direction of collision, but also on position along the slope. Sometimes, the value is as low as .445 radians (25.5 degrees). Other times, it will jump to .47 radians (around 27 degrees). I doubt a floating point error could give such a large variance, so there must be something else going on here