How to check if there is an object in front of my character

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

So i’m trying to make a classic final fantasy style like game here but the difference is the movement of the character is not limited by grid (limited by grid : i.e. press ui_right and the char will move 16px to the right or something like that, mine is not limited).

Since i’m new to godot i don’t really know how to do things in here
For every object has an Area2D and StaticBody2D for example:

Object TeddyBear
Object TeddyBear link to imgur

The Area2D is the active area of the teddy bear, the size are the same as the collision in StaticBody2D and collision in Area2D

Now what i want or how i want to do things may look like this:

var playerPos = Player.position
if(direction == "TOP"): playerPos += Vector2(0,-32)
elif(direction == "DOWN"): playerPos += Vector2(0,32)
elif(direction == "LEFT"): playerPos += Vector2(-32,0)
elif(direction == "RIGHT"): playerPos += Vector2(32,0)

//this is where i get stuck, i don't know the next code but, i want it more or less like this

if (playerPos is inside teddy.Area2D.collisionShape): canInteract = true

Am i doing things right or not? Please correct my way to do this if i am wrong or if this way is right then please help me on how to code that problem above

I am a new game developer and before using godot i was using unity, so changing to this newly released godot v3.0 stable needs me to get used to it

Thanks :slight_smile:

:bust_in_silhouette: Reply From: gswashburn

Don’t know if this may help you, but for my game Batty Bat, I had to do something similar for my enemies (Hornets). I wanted the hornets to follow Batty Bat (player) if Batty Bat got to close to them. So I looked at the players (bat) position and enemies (hornet) position and calculated the distance. If the enemy was within the specified distance then I started moving the enemy towards the player. If the player got outside the specified distance then I stopped the enemy from moving towards player. I counted the number of times the player got within the distance of enemy as well and if it reached a certain number of times then I made the enemy always follow the player. Ultimately if the player and the enemy overlap collision areas then the enemy is removed and the health of the player is subtracted.

The code is on the developers forum here: Loading...

Hope this helps,

I appreciate your help but that is not what i’m looking for, i’m afraid
I checked your script
The swarm there will explode when the body (player) enter the swarms area
Not really what i’m looking for, but i appreciate it
So thank you so much :slight_smile:

azureDev | 2018-03-07 14:06

Ohh and one more thing

At least i learn how to make object follows my player
It will be really helpful for my future game development since there is a scene where the enemy search the player like your swarm did there

So once again, thanks :smiley:

azureDev | 2018-03-07 14:08

In mine, you are correct, " The enemy shrinks away" but it doesn’t mean you can’t do something else instead. What are you trying to do when the player interacts with the Teddy Bear?

gswashburn | 2018-03-07 14:12

just simply show a dialogue box
it really is a simple problem actually but i just dont know how to do this in godot
“to interact with the object in front of you”
thats what i wanna do
with this area2D i have, the player only need to enter the area2D of the TeddyBear object and this cause a problem where i can “interact with the teddy bear” even though my player direction is not directed to the teddy
so its kinda… stupid… what i want to do to fix this is to enable “canInteract” if the player’s direction is directed to the object (teddy)
simple right? its actually a basic problem but i just dont get how can i do this in godot :confused:

azureDev | 2018-03-07 14:37

Nevermind, i found the answer already
Turns out everything can be found in the godot documentation
Facing Part

Anyway thanks again for your support :slight_smile:

azureDev | 2018-03-07 14:53