How can I use the function lerp for node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By megarubber

Hello everyone :slight_smile:

I created a shooting system on my game, and I would like the gun to follow the player with lerp()
How I can implement this?
I tried to call the lerp function, but the gun is too far from the player in the scene.

My code in the gun script (the gun is a node child from the player)

position.x = lerp(position.x, get_parent().position.x, 0.5)
position.y = lerp(position.y, get_parent().position.y, 0.5)

My scene:

:bust_in_silhouette: Reply From: rossunger

I think if you use global_position.x instead of each position.x that you have it should work…

Except that it will only ever move halfway toward the parent’s position… because you’re lerp weight is 0.5… also, I’m assuming you’re executing this code in a _process() function?

I think there’s probably a better way to do this… if the gun is a child of the parent, it’s always going to move with the parent…that’s how hierarchy works in godot. so no code is needed. and if you want the offset to be a set amount, then you just need to set the gun’s position to the direction that the player is facing.

thanks! It worked!

megarubber | 2022-02-07 00:57