0 votes

Hi there.
I have a RigidBody2D that it's falling, so i wanna it to not be affect by gravity.
But the problem is that i can't change the Default Gravity Vector in the project settings cuz that will affect other RigidBody2D in my game. And I'm also can't put it's Gravity scale to 0, because i wanna that body to be attract by a gravity point. (Area2D)

What should i do?

Godot version 3.2.3
in Engine by (73 points)

1 Answer

+1 vote
Best answer

You can set the gravity scale to 0 and still have it be affected by gravity points, to do that all you have to do is call a function that pulls the rigidbody into the gravity point if it enters the area

by (60 points)
selected by

Can u give me a example?

Im not very good with using godots built in function but i'd code the Area2D script somewhat like this

extends Area2D

func _on_RigidBody2D_body_enter(body):
    # gets global position of rigidbody and area2D
    var pos = self.get_global_position()
    var bodyPos = body.get_global_position()

    var target = pos - bodyPos

    # since target is a vector that points to the direction of the gravity point add_force(target) will move the body towards the gravity point
    body.add_force(target)

This code most likely wont work but im sure you can figure it out

It's give me the following error:

Invalid call to function 'add_force' in base 'RigidBody2D'. Expected 2 arguments.

Right, i forgot, use add_central_force(target) instead as add_force requires an offset vector too

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 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 webmaster@godotengine.org with your username.