Movement gone wonkers

geckoForcegeckoForce Posts: 17Member
edited January 2 in 2D

Is it an engines fault or it the code? Trying to apply a movement script to player character. The player is stuck in the air.

extends KinematicBody2D

var motion = Vector2(0,0)

const SPEED = 1000
const GRAVITY = 300
const JUMP_SPEED = 5000
const UP = Vector2(0,-1)


func _physics_process(delta):
    apply_gravity()
    jump()
    move()
    move_and_slide(motion, UP)


func apply_gravity():
    if is_on_floor():
        motion.y = 0
    else:
        motion.y += GRAVITY

func jump():
    if Input.is_action_just_pressed("ui_up"):
        motion.y -= JUMP_SPEED
    else:
        motion.y = 0

func move():
    if Input.is_action_pressed("ui_left") and is_on_floor():
        motion.x = -SPEED
    elif Input.is_action_pressed("ui_right") and is_on_floor():
        motion.x = SPEED
    else:
        motion.x = 0 

Comments

  • cyberealitycybereality Posts: 651Member

    That is kind of odd jump logic. See this sample i put together which should put you on the right track.

    extends Sprite
    
    var velocity
    var gravity
    var jump_speed
    var min_y
    var max_y
    
    
    func _ready():
        velocity = Vector2()
        gravity = Vector2(0.0, 52.0)
        jump_speed = Vector2(0.0, -22.0)
        min_y = 64
        max_y = get_viewport().size.y - min_y
    
    
    func _physics_process(delta):
        velocity += gravity * delta
        translate(velocity)
        position.y = clamp(position.y, min_y, max_y)
    
    
    func _input(event):
        if event.is_action_pressed("jump"):
            velocity = jump_speed
    
  • geckoForcegeckoForce Posts: 17Member
    edited January 2

    If I may, where can I learn that kind of code?
    Cause most tutorials I accessed have the jump (and movement) code same as I have used here.
    In all tutorials, Input.is_action_pressed("jump") is used.
    I see you are using the root node Sprite. Shouldn't it be a KinematicBody2D?
    Sorry I have beginner skills in coding.

  • cyberealitycybereality Posts: 651Member
    edited January 3

    Yeah, in a real game you would use KinematicBody2D if you're using a physics engine. I just used a sprite for a simple example.

    Handling sparse events (like jumping, shooting, etc.) is better if you put it in "_input()" and check the event properties. This only gets called when an event happens, rather than every frame in "_process()" where you check Input. Note for movement you still want to check in process for the smoothest response.

    I've been coding games since the 90's, so I have a lot of experience with character code. I just started with Godot like a month ago, but I feel pretty comfortable already. Most of game logic is the same in any engine, so I've just been getting up to speed with how things are done in Godot (won't say I'm an expert yet, I am still learning many things).

  • geckoForcegeckoForce Posts: 17Member

    What are some of the games you have coded? I guess you have a career in coding games.

  • geckoForcegeckoForce Posts: 17Member
    edited January 2

    Thanks for help.

  • cyberealitycybereality Posts: 651Member
    edited January 3

    I was mostly coding 2D Flash games in the early 2000's. Guess my claim to fame was this Batman game I made for Cartoon Network. Also did work for Adult Swim, MTV, and Nickelodeon. Been kind of out of it since Flash died, but I've been experimenting with 3D engines for a while and hope to release a mobile game this year. Thanks for your kind words.

  • geckoForcegeckoForce Posts: 17Member

    Your are really impressive! Didn't know there were so many awesome people here. I regret that flash is not gonna continue. I heard it is easy to hack. I like flash games and spent a lot of time on newgrounds. Some really innovative flash stuff there.
    All the best for your mobile game endeavour. Do share your game dev journey here.

    NB - I just want to know... are there any specific resources you are using to learn Godot or just the docs? I intend to release a game as well so that I can make some bucks.

  • cyberealitycybereality Posts: 651Member
    edited January 3

    Well, I started by reading the 3 Godot books on Amazon. I wrote reviews here if you want to check out:
    https://cybereality.com/category/godot/

    Godot Game Engine Development in 24 Hours was written by one of the co-creators of Godot, and it's a great place to start. Godot Engine Game Development Projects was my favorite though, as it shows how to create more complex projects (still demos but beyond the code snippets you see in the docs). The third book GDScript wasn't horrible, but it's not to the same level as the first two I mentioned.

    Otherwise I have just been reading the docs and searching for stuff on Google. The only problem is that the API has changed over the years, so sometimes Google hits talk about stuff that no longer exists. So you do have to check the docs to see if old functions or classes have been replaced.

    I haven't used too many tutorials yet, but there is a lot of great content I've seen or skimmed through.

    In terms of my own game, I'm just in the early stages now so I don't have anything to show. I do plan to share more once I get to a good place (going to GodotCon in February so hopefully I can get a demo together for then).

    Thanks again for your kind words.

  • geckoForcegeckoForce Posts: 17Member

    Well, I guess you are the one interactive with a coding noob so thanks to you.

  • geckoForcegeckoForce Posts: 17Member
    edited January 7

    II am trying to make a hurt response for my player. The player is moving along y-axis when hurt but does not moves away along x-axis. This is the function I call in _physics_process(delta) -

    func hurt():
        motion.y -= 1500
        motion.x -= 1500
        motion = move_and_slide(motion, UP)
        $AnimatedSprite.play("hurt")
    

    Can it be that the move() function along x-axis is conflicting with the motion.x in hurt() ?

  • cyberealitycybereality Posts: 651Member

    Yes, because you are setting the motion.x to a zero (or a fixed value) in move. You should try to avoid setting values directly, rather you should add or minus velocity. I can give you some sample code in a bit, I'm about to leave the house for a moment.

  • cyberealitycybereality Posts: 651Member
    edited January 7

    I updated my example to have the player bounce off the walls, and also made the velocity code more robust for sliding on the floor and faked friction. Note, this is just the basic idea, in a real game your would use collision objects, and functions like is_on_floor. I'm just hard-coding some values to keep it simple. Just add this code to a Sprite sized 128x128 to test it.

    extends Sprite
    
    var velocity
    var gravity
    var jump_speed
    var walk_speed
    var hit_speed
    var speed_limit
    var min_pos
    var max_pos
    
    func _ready():
        velocity = Vector2()
        gravity = Vector2(0.0, 52.0)
        jump_speed = Vector2(0.0, -22.0)
        walk_speed = Vector2(24.0, 0.0)
        hit_speed = Vector2(52.0, 0.0)
        min_pos = Vector2(64, 64)
        max_pos = get_viewport().size - min_pos
        speed_limit = 12.0
    
    
    func _physics_process(delta):
        velocity += gravity * delta
    
        if Input.is_action_pressed("move_left"):
            velocity -= walk_speed * delta
        elif Input.is_action_pressed("move_right"):
            velocity += walk_speed * delta
        elif abs(position.y - max_pos.y) < 2.0:
            velocity.x *= 0.895
        else:
            velocity.x *= 0.985
    
        velocity.x = clamp(velocity.x, -speed_limit, speed_limit)
    
        translate(velocity)
    
        if position.x < min_pos.x:
             velocity.x = hit_speed.x
        elif position.x > max_pos.x:
             velocity.x = -hit_speed.x
    
        position.x = clamp(position.x, min_pos.x, max_pos.x)
        position.y = clamp(position.y, min_pos.y, max_pos.y)
    
    
    func _input(event):
        if event.is_action_pressed("jump") \
                or event is InputEventScreenTouch:
            velocity.y = jump_speed.y
    
  • geckoForcegeckoForce Posts: 17Member

    Hey how are you doing? I made this game for a game jam in a few days.
    https://rxfactor.itch.io/tumblerobo

    Do check it out. Its not great but the theme was "Flop" - so.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file