Is this legit?

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

Hi everyone!!

New Godot user here, and so far loving it!

Just one question though…I have a 2d fighting game, street fighter type. I managed to solve the problem of flipping the character (independent scene) sprite so he always faces the oponent when jumping over each other. Both characters exist in the main scene as children of a generic Node (the white ones). The flipping is done in a script attached to both characters (the exact same script) where one character gets the other with a referenced variable at func _ready() using the get_parent().get_child() method, and then compares the x position of the oponent with his own x position (the comparation is done in a function under func _process(delta) ).

Here’s the thing…Is this legit? are there more orthodox methods? As I understand so far, the mantra is “signal up, call down”, so, maybe when dealing with “sibling” nodes, the interaction between them should be done in a script in the parent node? Or maybe using custom signals? However, I don’t think signals are the right tool when you need to compare two objects in relation to one another all the time, or maybe I’m wrong?

Anyway, any input shedding some light is more than welcome.

Thanks a lot!!

It makes more sense for the parent node to have the script. It can reference both children easily, plus if you have a lot of “get_parent.get_child()” in your script it could get rather confusing. :slight_smile:

Millard | 2020-08-06 15:40

Yeah, I thought the same, plus the the script for the player was getting really bloated.

Thanks!

yomato84 | 2020-08-07 13:10

:bust_in_silhouette: Reply From: sarthakroy

Well , you can use RayCast2D and its really easy . Create a RayCast2D to your player . If the enemy is within the RayCast2D arrow then your sprite is facing it by default. When the RayCast2D doesn’t detect the enemy (as player has gone beyond enemy ,then use (.flip_h) with Sprite and Raycast2d ,after which the player will turn facing the enemy.

Well, raycast2d makes a lot of sense, since that would eliminate the need to check position every single frame.
I will give it a try.

Thanks a lot!

yomato84 | 2020-08-07 13:08

If this helps then you can choose the best answer so that anyone who views the question gets to know .

I hope you will be able to make the raycast 2D work perfectly .
The Godot documentation is pretty good and you can check it if required.

All the best for your game !

sarthakroy | 2020-08-07 13:29

if the player or enemy moves on the y axis, would the raycast still pick them up?

Millard | 2020-08-16 04:03

It can if you use multiple ray cast . You can change the shape,size and direction of the raycast.

sarthakroy | 2020-08-16 04:06

oh, that makes sense.

Millard | 2020-08-17 22:00