1
\$\begingroup\$

I'm trying , to make a Player walk on a planet , through searching , I figured out that I can achieve this effect using Faux gravity , I tried to Implement it , but it's not working as intended:

Planet a StaticBody with a sphere collider:

extends StaticBody2D


var _gravity := 10.0

func attract(body : RigidBody2D, deltaTime):
    var gravityVector = (global_position - body.global_position).normalized()
    
    var rotation = Vector2.DOWN.angle_to((gravityVector))
    
    body.apply_central_impulse(gravityVector * _gravity)
    body.rotation = rotation

and the Player is a RigidBody2D with CustomIntegration turned on:

extends RigidBody2D

export var Attractor : NodePath
onready var attractor = get_node(Attractor)



func _integrate_forces(state):


    attractor.attract(self, state.step)
    
    var velocity = state.get_linear_velocity()

    if Input.is_action_pressed("ui_left"):
        state.set_linear_velocity(Vector2(-50, 0) )
    elif Input.is_action_pressed("ui_right"):
        state.set_linear_velocity(Vector2(50, 0) )
    

the Player seems to be rotated and attracted correctly , when I move from the Editor, but when I add a linear velocity to it , it start floating , how can make it work correctly?

\$\endgroup\$
4
  • \$\begingroup\$ I would try to dynamically change the direction of the gravity, depending on the position of the character w.r.t. the platform. I don't know how youd do it in godot, though. \$\endgroup\$
    – Vaillancourt
    Apr 29 at 11:04
  • 2
    \$\begingroup\$ Can you show us how you've tried implementing existing Q&A about how to implement this feature, including How do I make a character capable of walking on walls?, Make the player walk on walls in box2d, and How can I make a character walk on uneven walls in a 2D platformer? \$\endgroup\$
    – DMGregory
    Apr 29 at 11:15
  • \$\begingroup\$ question updated \$\endgroup\$
    – Abanoub
    May 31 at 16:07
  • \$\begingroup\$ 1. The speed should be perpendicular to the direction of gravity. 2. The speed should not be set directly, but a force should be added to the direction of movement, otherwise the effect of gravity will be overridden by the speed setting. 3. You should set a maximum speed. \$\endgroup\$
    – Mangata
    May 31 at 20:21

0

You must log in to answer this question.

Browse other questions tagged .