Player is unabled to climb slopes and jumps incorrectly.
Asked Answered
C

7

0

For some reason my player can't climb the slopes and their jump behavior feels very odd.

I have been working on the GDQuest tutorial series and this is one of their older ones. "Create Your First 2D Game With Godot 3 (extended edition)" is the name of it. It was originally done in 3.2.1 and I'm working 3.5.2, and I'm having some issues with my player behavior. I have been working pretty steadily though this course but got stumped on this over the weekend.

I have compared against the videos and Git repository but I'm just not seeing what I'm doing incorrectly. I have posted in the GDQuest Discord as well as now posting here now. If anyone could help me understand could help me understand as to why script is not as desired that would be appreciated.

I used some different assets an to give myself if some extra practice, I apologize if that causes any issues or discrepancy.

Link to .gif of the player unable to climb a slope.

Link to .gif of the jump behavior.

Link to Google Drive For .zip of my Godot Project.

Thank you for any help or direction as to how to resolve this.

Compendium answered 9/8, 2023 at 22:31 Comment(0)
K
0

Compendium Looks like a problem related with animations (i didn't download your project, i am on 4.1.1).
Maybe you can post here some line of your code related to character movements and a screenshot of your project.

Koralle answered 9/8, 2023 at 22:40 Comment(0)
C
0

Koralle Certainly!

Screenshot of the Player Animated Sprite

Player.gd - I tried to only post the related movement code.

extends Actor
onready var animated_sprite: Sprite = $AnimatedSprite
export var stompImpulse = 750.0
export var bumperImpulse = 1200.0
const SNAP_PIXELS = Vector2.DOWN * 65.0
var snapVector


func _physics_process(delta: float) -> void:
	var isJumpInterrupted = Input.is_action_just_released("jump") and _velocity.y < 0.0
	var direction = GetDirection()
	_velocity = CalculateMoveVelocity(_velocity, direction, speedLimit, isJumpInterrupted)
	# Disable snapping if the player wants to jump
	if direction.y == 0.0:
		snapVector = SNAP_PIXELS
	else:
		snapVector = Vector2.ZERO
	_velocity.y = move_and_slide_with_snap(_velocity, snapVector, FLOOR_NORMAL, true, 4, PI / 2.0).y
	Stomp()
	UpdateAnimatedSprite()


func GetDirection() -> Vector2:
	var playerGravity := 0.0
	if Input.is_action_just_pressed("jump") and is_on_floor():
		playerGravity = -1.0
	else:
		playerGravity = 1.0
	return Vector2(
		Input.get_action_strength("moveRight") - Input.get_action_strength("moveLeft"),
		playerGravity
	)


func CalculateMoveVelocity(
	linearVelocity: Vector2, direction: Vector2, speed: Vector2, isJumpInterrupted: bool
) -> Vector2:
	var newVelocity = linearVelocity
	newVelocity.x = speed.x * direction.x
	newVelocity.y += GRAVITY * get_physics_process_delta_time()
	if direction.y != 0.0:
		newVelocity.y = speed.y * direction.y
	if isJumpInterrupted:
		newVelocity.y = 0.0
	return newVelocity


func UpdateAnimatedSprite() -> void:
	if _velocity.x > 0:
		animated_sprite.flip_h = false
	if _velocity.x < 0:
		animated_sprite.flip_h = true

Actor.gd - This is the parent function that the Player extends.

extends KinematicBody2D
class_name Actor

const FLOOR_NORMAL := Vector2.UP
export var GRAVITY := 3000.0
export var speedLimit := Vector2(400.0, 650.0)
var _velocity := Vector2.ZERO


func _physics_process(delta: float) -> void:
	_velocity.y += GRAVITY * delta
Compendium answered 9/8, 2023 at 23:36 Comment(0)
M
0

Looks to me like there is a very big force pushing the Character down. Maybe this GRAVITY (3000?) value is too big and from one frame to the next is moving the Character many pixels down.

I would play with modifying this value.

By the way, fix the .zip link it is broken

Multiplicity answered 10/8, 2023 at 16:18 Comment(0)
M
0

I manage to make the character to move like this:

These are my changes:

Actor.gd:

diff --git a/Src/Actors/Actor.gd b/Src/Actors/Actor.gd
index f66507c..b12d563 100644
--- a/Src/Actors/Actor.gd
+++ b/Src/Actors/Actor.gd
@@ -2,8 +2,8 @@ extends KinematicBody2D
 class_name Actor
 
 const FLOOR_NORMAL := Vector2.UP
-export var GRAVITY := 3000.0
-export var speedLimit := Vector2(400.0, 650.0)
+export var GRAVITY := 400.0
+export var speedLimit := Vector2(100.0, 650.0)
 var _velocity := Vector2.ZERO

Player.gd

diff --git a/Src/Actors/Player.gd b/Src/Actors/Player.gd
index 696861a..34adad7 100644
--- a/Src/Actors/Player.gd
+++ b/Src/Actors/Player.gd
@@ -15,7 +15,9 @@ func _physics_process(delta: float) -> void:
                snapVector = SNAP_PIXELS
        else:
                snapVector = Vector2.ZERO
-       _velocity.y = move_and_slide_with_snap(_velocity, snapVector, FLOOR_NORMAL, true, 4, PI / 3.0).y
+       # I never has used "move_and_slide_with_snap" so I changed for "move_and_slide" for my tests
+       # _velocity.y = move_and_slide_with_snap(_velocity, snapVector, FLOOR_NORMAL, true, 4, PI / 3.0).y
+       _velocity = move_and_slide(_velocity, Vector2.UP)
        Stomp()
        UpdateAnimatedSprite()
 
@@ -23,6 +25,7 @@ func _physics_process(delta: float) -> void:
 func GetDirection() -> Vector2:
        var playerGravity := 0.0
        if Input.is_action_just_pressed("jump") and is_on_floor():
+               _velocity.y = -300 # On jump we don't change gravity we just give an Impulse UP to the Character
                playerGravity = -1.0
        else:
                playerGravity = 1.0
@@ -39,7 +42,9 @@ func CalculateMoveVelocity(
        newVelocity.x = speed.x * direction.x
        newVelocity.y += GRAVITY * get_physics_process_delta_time()
        if direction.y != 0.0:
-               newVelocity.y = speed.y * direction.y
+               # I don't understand the intention of this, I am "pass"ing it
+               pass
+               # newVelocity.y = speed.y * direction.y
        if isJumpInterrupted:
                newVelocity.y = 0.0
        return newVelocity

Hope you can continue from here.

As a tip, in my debugging, I have added a lot of print(...) commands to see how the variables are modifying their values.

Multiplicity answered 10/8, 2023 at 17:10 Comment(0)
C
0

Multiplicity Thank you for taking a look! I like your detailed reply and information. I'm always blown away by the technical knowledge and general kindness of people on this forum. Much appreciated friend.

I ended up changing my GetDirection() function. I updated the else statement and updated the playerGravity to zero rather than negative one.


func GetDirection() -> Vector2:
    var playerGravity := 0.0
    if Input.is_action_just_pressed("jump") and is_on_floor():
        playerGravity = -1.0
    else:
        playerGravity = 0.0 // This is the correct value it should have been 😒
    return Vector2(
        Input.get_action_strength("moveRight") - Input.get_action_strength("moveLeft"),
        playerGravity
    )

I'm certainly going to look at how reducing the gravity feels and plays. Thank you.

Compendium answered 12/8, 2023 at 14:14 Comment(0)
M
0

Compendium I'm happy you find yourself back on the right path. Implementing the character movement of a platformer is one of the most difficult things I have done as a hobbyist game dev. Don't despair. I would take a look at how other people are doing it. I found GDQuest people has a nice one about 2D Movement

Multiplicity answered 13/8, 2023 at 6:31 Comment(0)
C
0

Multiplicity Again just want to say thank you for your technical reply and kind words. Building platformers are bit more intricate that the internet would have one believe. Thank you and keep working on your projects and learning friend!🙂

Compendium answered 14/8, 2023 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.