Godot Version
4.3.stable
Question
I’m a beginner. I have a CharacterBody2D for the player, and I have a Polygon2D with a CollisionPolygon2D on it for the player to walk on and jump off of. I am trying to keep the player rotated so they are always “upright” and jump off the polygon appropriately. It seems to work at first, but as you see in my clip, when I move to one end of the polygon, the jumping no longer goes straight up but at an angle. You can see in this video. What am I doing wrong?
Here is the code for the player:
extends CharacterBody2D
@onready var world: Node2D = $".."
const SPEED = 300.0
const JUMP_VELOCITY = 400.0
var last_floor_normal
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity() * delta
var current_floor_normal = get_floor_normal()
if current_floor_normal:
rotation = get_floor_normal().angle() + deg_to_rad(90)
up_direction = current_floor_normal
last_floor_normal = current_floor_normal
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity = up_direction * JUMP_VELOCITY
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()