Hi,
I know the question has been asked about a year ago but I don't really want to reopen an old thread. Things may have changed with Godot 3.1 and to be honest I don't understand how to implement the answer.
I am doing a 2D game and I try to add a slide function to my player.
I want my slide function to
- stop responding to Input
- move following game physics to X position
- resume the input control.
So far I can only teleport to a specific location or move while in sliding position.
This is my code:
extends KinematicBody2D
const KUNAI = preload("res://scenes/Kunai.tscn")
var direction = 1
var camera_top = 0
var camera_left = 0
var camera_right = 4416
var camera_bottom = 1664
var camera_position = 25
var is_attacking = false
var is_running = false
var is_sliding = false
export var snap = false
export var move_speed = 350
export var jump_force = 550
export var gravity = 900
export var slop_slide_threshold = 60
var velocity = Vector2()
var destination = Vector2()
func _ready():
connect("finished", self, "on_finished")
$camera2D.limit_top = camera_top
$camera2D.limit_left = camera_left
$camera2D.limit_right = camera_right
$camera2D.limit_bottom = camera_bottom
set_process(true)
pass
func _physics_process(delta):
## Move ##
var direction_x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
velocity.x = direction_x * move_speed
print("velocity.x: "+str(velocity.x))
print("direction_x: "+str(direction_x))
## Running ##
if Input.is_action_pressed("ui_focus_prev") && velocity.x !=0:
is_running = true
if snap == true && is_sliding == false:
move_speed = 700
$sprite.frames.set_animation_speed("Run", 25)
camera_position = 35
else:
is_running = false
if snap == true && is_sliding == false:
move_speed = 350
$sprite.frames.set_animation_speed("Run", 15)
camera_position = 20
## Slide ##
if is_running and Input.is_action_just_pressed("ui_down"):
is_sliding = true
move_speed = 1000
$collisionshape2D.rotation_degrees = 90
if Input.is_action_just_released("ui_down"):
is_sliding = false
if $collisionshape2D.rotation_degrees == 90:
position.y = position.y-($collisionshape2D.get_shape().height/2)
$collisionshape2D.rotation_degrees = 0
## Jump ##
if Input.is_action_just_pressed("ui_select") and snap :
velocity.y = -jump_force
snap = false
# insert jump sound
## Set Gravity ##
velocity.y += gravity * delta
## apply movement ##
var snap_vector = Vector2(0, 32) if snap else Vector2()
velocity = move_and_slide_with_snap(velocity, snap_vector, Vector2.UP, slop_slide_threshold)
## prevent jumping ##
if is_on_floor() and not is_sliding and (Input.is_action_just_released("ui_right") or Input.is_action_just_released("ui_left")):
velocity.y = 0
## enable snap when landing ##
var just_landed = is_on_floor() and not snap
if just_landed:
snap = true
update_animation(velocity)
func update_animation(velocity: Vector2):
var animation = "Idle"
if abs(velocity.x) > 10.0:
$sprite.flip_h = velocity.x < 0
if is_sliding:
animation = "Slide"
else:
animation = "Run"
if not is_on_floor():
animation = "Jump" if velocity.y < 0 else "Fall"
if $sprite.animation != animation:
$sprite.play(animation)
Thank you