How can I put gravity and make the player jump using this script below?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hakal87

extends KinematicBody2D

export var speed = 75

func _physics_process(delta):
var direction: Vector2
direction.x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
direction.y = Input.get_action_strength(“ui_down”) - Input.get_action_strength(“ui_up”)

if abs(direction.x) == 1 and abs(direction.y) == 1:
	direction = direction.normalized()
var movement = speed * direction * delta
move_and_collide(movement)
:bust_in_silhouette: Reply From: nathanwfranke

In order to make gravity, you must store the player’s velocity in a variable and apply a downward force every frame.

For jumping, just set the velocity to face upward.

export(float) var speed := 75.0

var velocity: Vector2

func _physics_process(delta):
	# Gravity
	velocity += Vector2.DOWN * 10.0 * delta
	
	velocity.x = speed * (Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")) * delta
	
	if Input.is_action_just_pressed("ui_up"):
		velocity = Vector2.UP * 10.0
	
	move_and_collide(velocity)

sorry, it is not working, I can’t walk for the right and for the left and when I jump I kind of fly if you know what I mean. can you help me out please?

Hakal87 | 2020-04-26 17:49

Send entire code along with your scene (Screenshot would work)? Or you can send project as zip so I can look at it.

nathanwfranke | 2020-04-27 02:09