The character refuses to jump; how do I solve the problem? Here the code:
extends CharacterBody2D
const SPEED = 300.0
var JUMP_VELOCITY = 350.0
var movedir = Vector2(0, 0)
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
func _physics_process(delta):
movement(delta)
if not is_on_floor():
velocity.y += gravity * delta
func movement(d):
movedir = Vector2.ZERO #pour ne pas que le personnage continue de bouger
if Input.is_action_pressed(“left”):
movedir.x = -1
if Input.is_action_pressed(“right”):
movedir.x = 1
if Input.is_action_pressed(“jump”) and is_on_floor():
velocity.y = JUMP_VELOCITY
if Input.is_action_pressed(“down”):
movedir.y = 1
if Input.is_action_pressed("left") and Input.is_action_pressed("right"):
movedir.x = 0
if Input.is_action_pressed("up") and Input.is_action_pressed("down"):
movedir.y = 0
move_and_collide(movedir.normalized() * SPEED * d)
Up is negative y direction.
Also, velocity will only have effect if you use move_and_slide()
It’s either:
var JUMP_VELOCITY = -350.0
---
velocity.y = JUMP_VELOCITY
or
var JUMP_VELOCITY = 350.0
---
velocity.y = -JUMP_VELOCITY
And call move_and_slide()
I tried but it dosn’t work
extends CharacterBody2D
const SPEED = 300.0
var JUMP_VELOCITY = -350.0
velocity.y = JUMP_VELOCITY
Here the code and how can I reduce this ?
var movedir = Vector2(0, 0)
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
func _physics_process(delta):
movement(delta)
if not is_on_floor():
velocity.y += gravity * delta
func move_and_slide
movedir = Vector2.ZERO #pour ne pas que le personnage continue de bouger
if Input.is_action_pressed(“left”):
velocity.x = -1
if Input.is_action_pressed(“right”):
velocity.x = 1
if Input.is_action_pressed(“jump”) and is_on_floor():
velocity.y = -JUMP_VELOCITY
if Input.is_action_pressed(“down”):
velocity.y = 1
if Input.is_action_pressed("left") and Input.is_action_pressed("right"):
velocity.x = 0
if Input.is_action_pressed("jump") and Input.is_action_pressed("down"):
velocity.y = 0
move_and_collide(movedir.normalized() * SPEED * d)
Please check the guide I gave you so you know how to format your code properly, you did not.
I’m new you know I tried it and it didn’t work so I went back.
Please click on the guide and expand the section called “Format your code” and follow it’s steps.
I don’t see the format your code in the godot doc
It’s not in the godot docs. I sent you a link in the above post called “Posting guidelines in help channel”, please click it to open it.
You still aren’t calling move_and_slide, you should replace your move_and_collide with it and remove the function arguments.
'I tried but for no reason it doesn’t want to format the code