The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I was following the video "make your first 2d game with Godot" and while I was working on the part called "Preventing the velocity from resetting every frame" i noticed that my "player character" wouldn't jump after I looked threw the comments and found the line replacement for 13 on the player and it still wouldn't work.

https://www.youtube.com/watch?v=Mc13Z2gboEk&t=3625s

in Engine by (12 points)
edited by

Can you upload your project or at least the relevant portions of code? There can be a lot of reasons why something like a jump isn't working and I won't guess...

Player Code :
extends Actor

func physicsprocess(delta: float) -> void:
var direction: = getdirection()
velocity = calculate
movevelocity(velocity, direction, speed)
velocity = move
andslide(velocity, FLOORNORMAL)

func getdirection() -> Vector2:
return Vector2(
Input.get
actionstrength("moveright") - Input.getactionstrength("moveleft"),
-1.0 if Input.get
actionstrength("jump") and ison_floor() else 0.0
)

func calculatemovevelocity(
linearvelocity: Vector2,
speed: Vector2,
direction: Vector2
) -> Vector2:
var new
velocity: = linearvelocity
new
velocity.x = speed.x * direction.x
newvelocity.y += gravity * getphysicsprocessdeltatime()
if direction.y == -1.0:
new
velocity.y = speed.y * direction.y
return new_velocity

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
Actor Code:
extends KinematicBody2D
class_name Actor

const FLOOR_NORMAL: = Vector2.UP

export var speed: = Vector2(300.0,1000.0)
export var gravity: = 4000.0

var velocity: = Vector2.ZERO

below is what i did (notice there is a spelling mistake in calculate hece i changed the same in line 9)

extend the parent note aCTOR.gd

extends Actor

Load Physics (properties)

func physicsprocess(delta: float) -> void:

loading variable direction

var direction: = get_direction()

defining calclatemovevelocity sice it was crashing with parse error I declared it here, still the gravity isn't working

func calclatemovevelocity(speed, direction, velocity):

velocity = calclate_move_velocity(speed, direction, velocity)
velocity = move_and_slide(velocity)

func getdirection() -> Vector2:
return Vector2(
Input.get
actionstrength("moveright") - Input.getactionstrength("moveleft"),
-1.0 if Input.is
actionjustpressed("jump-up") and isonfloor() else 1.0
)

warning-ignore:unreachable_code

warning-ignore:unreachable_code

func calculatemovevelocity(
speed: Vector2,
direction: Vector2,
linear_velocity: Vector2
) -> Vector2:

new velocity

    var new_velocity: = linear_velocity
    new_velocity.x = speed.x * direction.x
    new_velocity.y += gravity * get_physics_process_delta_time()
    if direction.y == -1.0: 
        velocity.y = speed.y * direction.y

    return speed * direction

1 Answer

+2 votes

You mixed up the order of arguments: your calculate_move_velocity-function expects speed as the second and direction as the third argument, but you passed direction as the second one and speed as the third one instead.


For future questions please make sure that your code is formatted properly (each line has to start with 4 spaces, each additional 4 spaces equal an indentation layer). If you don't, underscores are interpreted as markdown and indentation is lost, which makes your script a lot harder to read for others trying to help you.

by (10,634 points)

Thanks it helped me as well

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.