Grab mechanic issue

Im trying to implement a mechanic where the player can grab an NPC and drag them around. Iv got it so that the NPC follows the player around but i cant get it to rotate with the player so that the NPC is always in front of the player.

Below code is what has the NPC maintain the appropriate distance from player.

 func _process(delta: float) -> void:
    if grabbed:
	    P_postion = get_parent().player_body.position
	    P_postion += Vector3(1.5,0,0)
	    self.transform.origin = P_postion

Any help is greatly appreciated.

Hello!

If I understand correctly what you want to achieve, you don’t actually need to write any computation in the _process() function. This computation is already managed by the tree system when you add a node as a child of another node.

You can create a function on_picked_up() in the NPC which will be called only once, that is when the player is requesting to pick up a NPC. In that function, you would simply add the NPC as a child of the player.

A child will automatically be following the parent, with its rotation.

func on_picked_up(player: PlayerNode):
    player.add_child(self)
    self.position = Vector(1.5, 0, 0)
    # note: you can also set the local rotation so the NPC faces the player

Hope that helps!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.