The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

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!!

in Engine by (16 points)

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 "getparent.getchild()" in your script it could get rather confusing. :)

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

Thanks!

1 Answer

0 votes
Best answer

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.

by (113 points)
selected by

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!

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 !

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

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

oh, that makes sense.

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 Frequently asked questions and 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 [email protected] with your username.