Godot Version
4.2
Question
Iam trying from along time to make the player can slop upwarding and rotating when go up but i coudn’t
if someone know how i can fix it please tell me here is my player code
extends CharacterBody2D
const speed : float = 600.0
const acc : float = 400.0
var friction : float = 1
@onready var player = $“.”
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var left_ground = $LeftGroundSensor
@onready var right_ground = $RightGroundSensor
@onready var left_wall = $LeftWallSensor
@onready var right_wall = $RightWallSensor
@onready var collision_shape_2d = $CollisionShape2D
@onready var ground_ray = $“…/RayCast2D”
var ground_normal : Vector2
jump and gravity
const jump_force = -500.0
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
func _physics_process(delta):
print(velocity.x)
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_force
Play animations
if is_on_floor():
if velocity.x >= -4 and velocity.x <= 4:
animated_sprite_2d.play("idle")
elif velocity.x >= 500:
animated_sprite_2d.play("run 5")
elif velocity.x >= 350:
animated_sprite_2d.play('run 4')
elif velocity.x >= 150:
animated_sprite_2d.play('run 3')
elif velocity.x >= 10:
animated_sprite_2d.play('run 2')
elif velocity.x <= -500:
animated_sprite_2d.play("run 5")
elif velocity.x <= -350:
animated_sprite_2d.play('run 4')
elif velocity.x <= -150:
animated_sprite_2d.play('run 3')
elif velocity.x <= -10:
animated_sprite_2d.play("run")
else:
animated_sprite_2d.play("jump")
move_and_slide()
#regular movement
func apply_traction(delta):
var traction: Vector2 = Vector2()
if (Input.is_action_pressed("moveRight")):
traction.x += 1
animated_sprite_2d.flip_h=false
if (Input.is_action_pressed("moveLeft")):
traction.x -= 1
animated_sprite_2d.flip_h=true
traction = traction.normalized()
velocity += traction * acc * delta
func apply_friction(delta):
velocity.x -= velocity.x * friction * delta
func _process(delta):
apply_traction(delta)
apply_friction(delta)