Answers for "2d platformer movement godot"

2

gdscript player controller 2d

extends KinematicBody2D

var velocity = Vector2.ZERO

func _physics_process(delta):
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	
	if input_vector != Vector2.ZERO:
		velocity = input_vector
	else:
		velocity = Vector2.ZERO
	
	move_and_collide(velocity)
Posted by: Guest on August-03-2020
0

simple platformer movement in godot

# simple platoformer movement for godot with friction and acceleration
# "move_right", "move_left" and "move_up" are defined in project setting/input map

# the node that we'll use is kinematic body 2d
extends KinematicBody2D

# the movement variables (mess around with them to make the movement the way you like)
export (int) var speed = 300
export (int) var jump_speed = -600
export (int) var gravity = 1000
export (float, 0, 1.0) var friction = 0.25
export (float, 0, 1.0) var acceleration = 0.25

var velocity = Vector2.ZERO

# geting player input with our own function
func get_input():
	var dir = 0
	if Input.is_action_pressed("move_right"):
		dir += 1
	if Input.is_action_pressed("move_left"):
		dir -= 1
	if dir != 0:
		velocity.x = lerp(velocity.x, dir * speed, acceleration)
	else:
		velocity.x = lerp(velocity.x, 0, friction)

# and finaly calculating the movement
func _physics_process(delta):
	get_input()
	velocity.y += gravity * delta
	velocity = move_and_slide(velocity, Vector2.UP)
	if Input.is_action_pressed("move_up"):
		if is_on_floor():
			velocity.y = jump_speed
	if Input.is_action_just_released("move_up"): # this will check to see if are jump key is released and stop the player jumping
		if sign(velocity.y) != 1:
			velocity.y = 0
Posted by: Guest on December-04-2020

Browse Popular Code Answers by Language