How do i flip a node 2d and when it is over 90 degrees or less

Godot Version

4.2.2

Question

making a node 2d flip when it is over 90 degrees of rotation or less. this is for a 2d shooter like portal 2d

My current code

extends Node2D

func _process(delta):
look_at(to_global(to_local(get_global_mouse_position()).rotated(PI/2)))

Might be better to first get the angle to the mouse, then you can do some flipping and adjust the angle.

var mouse_angle := global_position.angle_to(get_global_mouse_position())
flip_h = mouse_angle > PI

# reduce mouse_angle
mouse_angle = fmod(mouse_angle, PI)
# flip mouse_angle if applicable
mouse_angle *= -1 if flip_h else 1

# apply extra rotation? Not sure what this was doing originally.
rotation = mouse_angle + PI/2

May have implimented the code wrong but its giving me identifier I aint the brightest in gd script so I do not know the actualy identifiers

Right, I’m sure the error is that it is unindented code. To be part of the _process function it will need 1 tab at the start, kind of like line 6

oh I think i found the issue

1 Like

what do you want to flip? flip_h is a Sprite2D property, do you want this script attached to your sprite, or is the script a parent of your sprite?

No it says that flip_h is i think bassicly outdated

It’s not a property of Node2D, but it is a property of Sprite2D. The script only looks at it’s own properties, so it can’t find flip_h if it’s only extending Node2D. Where is your sprite? You can access the sprite’s properties in other ways.

wait I solved it but its giving me an error on that parameter delta is never used in function "_process()

That’s likely a warning, don’t worry about it. The red dot in the “gutter” by the line numbers is a breakpoint, it will always stop at that point until you click the red dot to remove it.

You need to use the dollar sign to get child nodes, if you sprite is named “Gun” this will work.

After trying the script myself, I think you actually want this script, I was over complicating it.

var mouse_angle := (get_global_mouse_position() - global_position).angle()
$Gun.flip_v = mouse_angle > PI/2 or mouse_angle < -PI/2
$Gun.rotation = mouse_angle
1 Like

it gives me error that unexpected $ in class body.

again, it will need indentation.

1 Like

well there are a few glitches that are easy to patch but it works

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.