How to push and pull objects

Godot Version

godot 4.0 stable

Question

How do i make a player push and pull objects in a top down game?

I think that you could make a function inside the object you want to pull with the direction as parameter. Then, when the player walks to the object direction for maybe 0.2 sec, you could call the function with the diference of positions normalized and multiply it by the distance you want to move.
You can also do it by apliying forces to a RigidBody (2D or 3D).

i tired to make the box follow the player when he enters the area and presses a button but it just gliched and did not work

var motion = Vector2(0,0)
var speed = 175
var go = false
@onready var p = $“…/player/player2d”

func _ready():
pass # Replace with function body.

func _process(delta):
if Input.is_action_just_pressed(“yes”) and go == true:
motion = (p.position) * speed
move_and_collide(motion)

func _on_rigidarea_body_entered(body):
go = true # Replace with function body.

func _on_rigidarea_body_exited(body):
go = false # Replace with function body.

1 Like

Could you provde more information about what do you want to make?
Do you want it to follow physics?
Do you want it to be like for a puzzle game?
Please, give more information

Anyways, as I understood your code:
You could give to the go variable the body that enters on the area, and then, when the action "yes" is pressed, you could tween it’s position or aply it a force. Smt like this:

func _procces():
   if go and Input.is_action_just_pressed("yes"):
      go.velocity = (go.position-position).normalized()*50

i dont know about physics but its kind of top down like what you would find in undertale or oneshot
and yes it is meant for a puzzle

Ok, then made what I said but instead of using forces use a tween so that it doesn’t bugs

What type of node is the object? CharacterBody or RigidBody?

func _process(delta):
	if go and Input.is_action_pressed("yes"):
		var tween = create_tween()
		tween.tween_property(self, "position", p.position - self.position, 0.1)

this code kind of works but the objects is slower than the player
is there a way to make it so that when the button is pressed the object will move the same distance as the player

it is a rigidbody but i dont think it being a rigidbody is useful since the game is a top down so physics are not that important

Just reduce the last parameter of the tween (time)

You can also normalize the distance and multiply it.

Physics is important no matter the view. For your use case, I would not recommend tweening the position of a rigidbody since push and pull is something that needs to be done over many frames and that can mess with the physics system. Read the docs here

You have two options, apply forces or use the custom integrator. To use custom integrator, add this function to the rigidbody script. It will use the players velocity (if it’s a CharacterBody) to safely change the position of the rigid body.

func _integrate_forces(state):
state.transform.origin += player.velocity * state.get_step()

so what happens now is when the player collides with the box it sticks to the player until he stops moving but the bigger issue happens when i try to push the box down both the player and the box start falling down as is gravity existed

i have this code on the player to push the rigidbody maybe it has somthing to do with it


	for i in get_slide_collision_count():
		var c = get_slide_collision(i)
		if c.get_collider() is RigidBody2D:
			c.get_collider().apply_central_impulse(-c.get_normal()*push)


Following my experience, ignore the physics make it work. I don’t know how do that affect to the performance but maybe @FreakyGoose has reason and is better use physics. Anyways, if you would like to make a puzzles game I would recommend you tweeting.

What do you mean by “…player and the box start falling down as is gravity existed”? I thought you said it is a top-down game?