#need help character floating in air with gravity applied

1 messages Ā· Page 1 of 1 (latest)

coral egret
#

Hey everyone šŸ‘‹
I’m working on a simple platformer in Godot 4 and running into a weird issue:

My player starts floating slightly above the ground.
Jumping works strangely .

I’m using CharacterBody2D with move_and_slide() and gravity.

here's my charcter script :

extends CharacterBody2D

@onready var sprite = $AnimatedSprite2D

var speed = 150
var jump_force = -500
var gravity = 1200  # Stronger gravity = no floating

func _physics_process(delta):
    # Apply gravity pulling down
    velocity.y += gravity * delta

    var on_ground = is_on_floor()

    # Right movement
    if Input.is_action_pressed("right"):
        velocity.x = speed
        if on_ground:
            sprite.play("run")
    else:
        velocity.x = 0
        if on_ground:
            sprite.play("idle")

    # Jumping
    if Input.is_action_just_pressed("space") and on_ground:
        velocity.y = jump_force
        sprite.play("jump")

    # Stay in jump animation while airborne
    if not on_ground:
        sprite.play("jump")

    # Apply movement
    move_and_slide()
    if is_on_floor():
        print("Standing on ground at X:", position.x)
    else:
        print("In air at X:", position.x)


its so simple it shouldnt cause any issue.
any help would be appreciated
thanks

fresh garnet
#

Why are there 2 of each script?

coral egret
#

@fresh garnet I started with some more complex versions of the scripts, but I ran into multiple issues (like snowballs only spawning in one vertical spot and the character floating above the ground). so i started to create simple movements from start but encountering same issues.snowball script is just empty the one which is attached . may be issue is not in script . if yk what causes such issue please let me know

fresh garnet
#

i cannot really tell what is happening from the video
Could the character be falling into the void with the background being stuck into view?

hollow ledge
#

My guess it is something with that parallax effect. Can you try it with some normal background.

coral egret
coral egret
#

but i want the snowman to feel like its moving and not in one position and the movement should feel real . how can i achieve that ? (without parallax node which is causing issue )

and one more qs is it some change by me in parallax thats causing this behaviour or it works this way only?

hollow ledge
#

How you make the camera follows player position?

coral egret
#

@hollow ledge by adding camera2d node to player node, this way i do it. are there any other ways too?

hollow plover
hollow ledge
#

I don’t really recommend to make a camera in the player node (except for first person view)
You can make it outside and have reference to player via export variable, this way you can customize how the camera would behave when player move
Then you can set that the camera only move when player moves horizontally, so the player jump it will actually looks jump instead of locked in the center of the screen

coral egret
# hollow plover At least to me, this looks like more of an issue with the parallax origin, and n...

video showing it working without the background will help us to get the effect you're looking for.
yeah sure i will send. but right now dealing with godot crash ig some gray color on menu side and when i hover over it texts are moving right left . dk what caused this . i will fix it first then show how its working without bg .
Do you have a separate texture for the background and ground sprite?

yeah i do have separate texture for bg actually 3layers of bg but ig i am not using separte texture for ground just using collider because in foreground i drew ground. do you think this can be reason for the issue?

coral egret
hollow plover
#

Alright, as far as the camera, This should be everything you need for now. You can add more to it later, but this should give you a good starting point. You probably can't copy paste that code tho. You'll need to make an actual reference to the player (which we can help you with later.)

As far as what I think is happening with the parallax background, here's a quick demo I threw together to show what I think is happening. In my scene, I have everything set up a bit different (you can still have a custom scene for the parallax layer, but you should change the root node to an actual parallax background node. This node gives you the ability to have an offset. What I think is happening is that the sprites for the background just aren't quite lined up with where the ground should be. The simplest fix for this is changing the offset like I show in the video. If this doesn't help, let me know!
https://youtu.be/kn7z1oy9llg

coral egret
hollow plover
#

Let me know if you still have problems afterwards. We'll get your game up and running gdsalute

coral egret
hollow plover
coral egret
#

i just run game , recently it was because of tileset node may be . i am not sure, my hardware is bit older so dont support vulkan , as godot uses vulkan api ig . @hollow plover

fresh garnet
fresh garnet
#

Or have a laptop with a mobile GPU

coral egret
coral egret
fresh garnet
# coral egret on pc with its integrated gpu,

Then you should not be using that render, it is only for mobile GPUs.
When a mobile device runs the game, it will automatically select the mobile renderer. But otherwise you should stick to a renderer that YOUR current device supports.

#

Not saying it is a bad idea to test other renderers ocassionally. But it will never be 100% accurate to how it would look in an actual phone.

coral egret
fresh garnet
#

It is not "needed" exactly. But it is simply not made for desktop GPUs, which work differently.

Mobile ones work in screen sectors and batch drawing differently, meant to use less power but being less efficient than a proper GPU. If you try to use that on a desktop environment, you'll just get worse performance as your GPU has to work with stuff that was not optimized for it.

coral egret
#

my device uses - Mesa Intel(R) HD Graphics 4600 (HSW GT2), what do you think which renderer i should be using ?

fresh garnet
#

Compatibility

#

Any integrated GPU should run with Compatibility.
Newer CPUs don't even have integrated GPUs, so they are all old.

coral egret
#

ohh thanks nancok i will switch my renderer mode and see if it works. thanks you so much for taking time to ans my qs . its new to me to know all this so kinda interesting. i will try compatibility mode and see if it works .

#

one more qs , should i not use parallax layer for bg? it was causing floating issue .

fresh garnet