0 votes

I have an infinite runner game set up, the idea is that you can flip gravity. But if I set my gravity variable to anything higher than 50, the player object sinks into the ground. The player is a RigidBody2D in character mode (because I couldn't figure out collisions for a KinematicBody2D), and the ground is made of StaticBody2D tiles. Here is the player movement script:

func _input(flipGravity):

    if Input.is_action_pressed("flipGravity") and gravityFlipped == true:
        motionVector.y = 0
        gravityFlipped = false;
        motionVector = Vector2(speed, gravity)

    elif Input.is_action_pressed("flipGravity") and gravityFlipped == false:
        motionVector.y = 0
        gravityFlipped = true;
        motionVector = Vector2(speed, -gravity)

func _fixed_process(delta):
    var playerPosition = get_node("player").get_pos()
    playerPosition += motionVector * delta


    speed += 0.01
in Engine by (24 points)

3 Answers

+1 vote
Best answer

The problem is you're setting the player's position, and not actually moving them.
set_pos() teleports the object to that position, and does not consider collision/physics while doing so.

To move your rigidbody, use set_linear_velocity() or apply_impulse. Though it's not a good idea to move a rigidbody this way in fixedprocess. Your best bet is to enable Custom Integration and move your player within that.

by (1,330 points)
selected by
0 votes

Do not increase the gravity higher than 50 ...

Increase the fps of the physical engine in the settings.

by (208 points)

This sadly does not work, as I would need to set the fps so high that it starts to cause performance issues to get the gravity high enough. I also tried increasing the gravity variable after setting the fps higher, that had a slight effect but not strong enough.

0 votes

I managed to solve this myself by instead of changing the player position I modify the object's gravity scale. This also adds some difficulty to the gameplay, which is nice.

by (24 points)
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.