0 votes

hi i am currently making a simulation game involving creatures and i have this KinematicBody2D with the "Sight" Area2D attached and i have a piece of fruit is there anyway i can get the position of the fruit once it enters the area ( i am using onbodyentered) without it telling me via a file or something because i realised that if i want to use files i would have to have a different file for every creature and piece of fruit but then i would have to send the file path in a file and then to make that file random i would have to make yet another file etc. please let me know thanks.

in Engine by (18 points)

1 Answer

+3 votes
Best answer

if you don't know, the function OnBodyEntered gives you the body that entered, so you can tell it to set position of the body, so you type:

var position = Vector2.ZERO
if body.name == the name of the body:
    position = body.global_position

and if you want to get several positions without creating a lot of variables, you can create an array:

var positions = []
 if body.name == the name of the body:
    positions.append(body.global_position)
by (286 points)
selected by

would this work similarly on an instanciated object?? because fruit in this case is in a different scene entirely and will be spawned in.

sorry i should have made that clearer i my original comment.

yes, it should work.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.