extends CharacterBody2D const SPEED = 150.0 const SPRINT_MAX = 2 # Get the gravity from the project settings to be synced with RigidBody nodes. #var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") var sprint = 1 var gravity = 0 var turning = false var anim var sprite var direction = Vector2.ZERO func _ready(): anim = $AnimationPlayer sprite = $Sprite2D func end_turn(): sprite.flip_h = !sprite.flip_h turning = false anim.speed_scale = 1 print( "turned" ) func turn(): #turning = true anim.speed_scale = 1 anim.play("turn") func _physics_process(delta): # Add the gravity. if gravity !=0 and not is_on_floor(): velocity.y += gravity * delta # Handle sprint. if Input.is_action_just_pressed("ui_accept"): sprint = SPRINT_MAX else: sprint = move_toward( sprint, 1 , 0.01 ) # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. direction = Vector2 (Input.get_axis("ui_left", "ui_right"),Input.get_axis("ui_up", "ui_down")/2).normalized() if (Input.is_action_just_pressed("ui_right") and sprite.flip_h) or (Input.is_action_just_pressed("ui_left") and !sprite.flip_h): turn() pass if direction != Vector2.ZERO and !turning: if ( direction.x < 0 and sprite.flip_h ) or ( direction.x > 0 and !sprite.flip_h ): sprite.rotation = 0; velocity.x = direction.x * SPEED * sprint if sprint > 1: anim.speed_scale = sprint anim.play("sprint") else: anim.speed_scale = 1 anim.play("swim") velocity.y = direction.y * SPEED if !sprite.flip_h: sprite.rotation = move_toward(direction.angle(),0, .1) else: sprite.rotation = move_toward(direction.angle() - PI,0, -.1) else: velocity.x = move_toward(velocity.x, 0, SPEED) velocity.y = move_toward(velocity.y, 0, SPEED) anim.speed_scale = .5 sprite.rotation = move_toward(sprite.rotation,0, .1) $AnimationPlayer.play("idle") move_and_slide()