#How do I get an area node to follow a character node

95 messages · Page 1 of 1 (latest)

graceful sinew
#

I'm new.

grim grove
#

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?

graceful sinew
#

like it goes to the player

#

every frame

kind furnace
# graceful sinew like it goes to the player

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

graceful sinew
#

isn't this just making the node have the same position as the player

#

@kind furnace

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

graceful sinew
#

yeah

#

travel

kind furnace
#

👍 okay let me explain:

graceful sinew
#

i did it before

#

but

#

it was a lot of code

kind furnace
#

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)

kind furnace
#

you might have to mess with the speed variable a bit to get it to work

graceful sinew
#

what do i set area and playerto

kind furnace
#

"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

kind furnace
#

dont know why i made it so word-y lol

graceful sinew
#

this?

kind furnace
#

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

graceful sinew
#

it didnt work

#

@kind furnace

kind furnace
#

did it give an error? did the area just not move? @graceful sinew

graceful sinew
#

it didnt move

kind furnace
#

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.

graceful sinew
#

@kind furnace why is it 3D

kind furnace
graceful sinew
#

the mesh

kind furnace
#

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.

graceful sinew
#

it is

#

blue

#

and i have a sprite

#

@kind furnace

kind furnace
#

it shouldnt be 3d. at the very top of the godot interface, make sure you have 2d selected

graceful sinew
kind furnace
#

just do a box mesh for now

graceful sinew
#

but my game osnt 3D

#

@kind furnace

kind furnace
#

yeah, you can have a 2d mesh

#

let me open godot and show you

graceful sinew
#

a box is 3d

kind furnace
#

just click it lol

#

it will make a 2d square

#

in my case it did

graceful sinew
#

it did

#

still did noth8ing tho @kind furnace

kind furnace
#

ah just remove the mesh then, make a sprite2d instead

graceful sinew
#

i already had one

kind furnace
#

oh then nvm

#

my bad

graceful sinew
#

its okay

kind furnace
#

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

graceful sinew
kind furnace
#

my bad, make Vector2 capitalized

#

the V in Vector2 specifically-

graceful sinew
#

the thing disappeared

#

when i ran it

kind furnace
#

did it go to 0, 0?

#

try decreasing the speed to a very low value

graceful sinew
#

oh

#

yeah it went to zero zero

#

*traveleed

kind furnace
#

you could've also set the player variable wrong somehow, lemme go back

kind furnace
# kind furnace

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.

graceful sinew
#

I fixed it

kind furnace
#

👍

#

congrats man

graceful sinew
#

the onready variable was grabbed froma different scene