Godot Version
4.3 Stable
Question
Hello! I want to flip the character (Not flipping as in changing the scale to -1, I want for the character to rotate, like how Sonic jumps in the 2D games, but just with the sprite), depending on which direction the player is running. I found out a way to lerp the rotation to 0 degrees, but it lerps to the nearest rotation.
When it rotates to the nearest 0 degrees, it doesn’t look very natural. How can I make it so that the square rotates depending on the player input? Here’s my code:
extends CharacterBody2D
@export var MAXSPEED = 750.0
@export var JUMP_VELOCITY = -600.0
@export var acceleration = 37.5
var isOnFloor
# 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 !is_on_floor():
velocity.y += gravity * delta
isOnFloor = false
else:
isOnFloor = true
# Handle jump.
if Input.is_action_pressed("Jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
var inputDirection = Input.get_axis("Left", "Right")
if inputDirection:
velocity.x += acceleration * inputDirection
else:
velocity.x = lerp(velocity.x, 1.0, 0.02)
velocity.x = clamp(velocity.x, -MAXSPEED, MAXSPEED)
move_and_slide()