#How do I get an area node to follow a character node
95 messages · Page 1 of 1 (latest)
Hi there! So depends on what the goal is, could you elaborate on what your trying to do? Do you mean like having the area node change position along with the character? Or like an NPC that follows the character lagging behind some distance?
Alright here is what you could do.
1: Make the Area a child of the player, causing it to update positions along with the player.
2: In the _process(delta): or _physics_process(delta): functions, you can write the line:
Area.global_position = Player.global_position
_process(delta): is called every frame while _physics_process(delta): is called a consistent 60 times a second. Process looks smoother but physics process will technically be more accurate no matter how laggy or fast your PC is. For this case, I would use physics process as an area is normally not visible during gameplay.
Ping me if you need more help @graceful sinew
isn't this just making the node have the same position as the player
@kind furnace
Thats right
what do you mean "goes to the player"?
I interpreted your question as a teleport every frame
do you want it to slowly travel to the player? I can tell you how to do that
@graceful sinew
👍 okay let me explain:
var speed = 4.0
func _process(delta):
Area.global_position = Area.global_position.lerp(Player.global_position, delta * speed)
should work
"lerp" is also known as linear interpolation.
it is a function that can be used in several ways to make one value (in this case, the position of your area) slowly reach the value of another (player position)
is this what youre looking for?
you might have to mess with the speed variable a bit to get it to work
what do i set area and playerto
"Area" is a reference to the area node. If this script is attached to the Area, then you dont need to type area and you can just put global_position instead.
"Player" is a reference to the player, so you can say:
@onready var player = $Player (Drag in player from the scene tree)
@export var player: KinematicBody2D (Works in 3D too, assign player in the inspector)
i recommend the onready method
so in short, area is a reference to the area node, and player is a reference to the player node
dont know why i made it so word-y lol
yes, that should work
you can mess around with the speed variable if needed
theres also a way to make Areas visible when testing if you need
@graceful sinew
probably a mistake on my part, let me test some things out real quick
did it give an error? did the area just not move? @graceful sinew
it didnt move
try setting the speed to like 400 or something
i did put it in to multiply by delta which is a ridiculously small value on its own
this is a simple recreation scene and it works fine. The speed is a bit high tho-
@graceful sinew
try adding a child mesh or texture to your area so you can properly see whats going on, if you havent already.
@kind furnace why is it 3D
Why is what 3d? Did you put a 3d mesh instead of a 2d one by accident or something?
the mesh
ah make sure you add a MeshInstance2D (the icon should be blue) instead of a MeshInstance3D
select the mesh and in the inspector, make a new box mesh. It should be a square that you can drag to make larger. You could also do a sprite2d if you have a texture to use instead.
it shouldnt be 3d. at the very top of the godot interface, make sure you have 2d selected
just do a box mesh for now
a box is 3d
ah just remove the mesh then, make a sprite2d instead
i already had one
its okay
in your code:
global_position = global_position.lerp(vector2(0, 0), delta * speed)
try this instead to ensure its not an issue with the position it is trying to get.
this should cause the mesh to travel to the origin of your map, level, whatever it might be
okay thats a step in the right direction. set Vector2(0, 0) back to player.global_position in that script
you could've also set the player variable wrong somehow, lemme go back
once again look at this example scene i made. Try dragging your player, hold control, then release into the top of your script to get a reference in case thats the issue.
I fixed it