Object won't collide when its global_position is changed.

Godot Version: 3.5.1

I have to KinematicBody2Ds. The player, and a object which when colliding with player can be used to move the player. This is the only way the player can move. I have another KinematicBody2D which is supposed to be a wall that both of the other objects collide with. I use this code to move the player:

var Velocity = Vector2()
var Speed = 200
func _physics_process():
	if CanIBeDraggedLR == true: 
		if Input.is_action_pressed("ui_left"):
			Velocity.x = -Speed
		elif Input.is_action_pressed("ui_right"):
			Velocity.x = Speed
		else:
			Velocity.x = 0
		move_and_slide(Velocity)

With the other object, I make its velocity and global_position the same as the player. With this, the other object doesn’t collide with the wall. Without changing the g.p. it collides and gets pushed back. How can I keep the other object in the same desired position (center of player) but also have it collide with other objects?

Directly setting the Position or Global_Position of objects will cause them to ignore any physics based detection.

To keep the other object in the same position as the player then I recommended you make it a child of the player.

1 Like

Thank you! But I forgot to mention, the other object needs to move independently. I have a video which I should posted in the first place (this is without doing anything with the global position btw):

I think you’re going to need to call move_and_slide or move_and_collide for both; since the player can only move when pushed by the other object, I would use the move_and_collide method of the other object to impart a velocity on the player.

Oh, forgot to mention, the player isn’t acted on the other object. When the other object is on the player and the little button on top of it is pressed, it allows the player to move us the arrow keys.

If I understand what you’re trying to do; when the object is “attached” to the player, I would (1) make the object a child of the player, so that it will move with the player; (2) make them have collision exceptions with each other so that you don’t have them interfere and don’t have to think about changing collision layers, and (3) then give the player a duplicate of the object’s collider, so that the whole player-collider unit collides when the object would collide.