#Making Graphic Rotate Based on Menu Selection
1 messages · Page 1 of 1 (latest)
Hello, I've been setting up this main menu for my game, and I'd like to be able to set this graphic of the main character to rotate so that he points at whatever option currently has focus (using keyboard/controller input), preferably with a relatively smooth ease-in motion. Is there any particular good way to do this?
You could attach focus_entered signals to the buttons you want your character to point to. Use the Advanced option when setting up the signals to add a Float as an extra call argument and make that value the angle in degrees you want the character rotated to so they point at that button. Attach all the singals to the same function which sets a target angle variable based on the returned angle from the signal. Then use lerp_angle in your physics process to rotate the character from their current rotation to the target rotation. You would need to set up your character so they are initially pointing directly right when their rotation is 0 degrees and their pivot point is placed where the knot of their tie is. The button rotations would then be negative, with the rotation to the New Game button being around -85.0 for example.
ah i see. i know how to attach signals to buttons, but looks like i will have to go and learn most of the rest of that stuff
i am very much a beginner so bear with me here:
- how do i attach multiple signals to one function?
- how would i make it set a target angle variable based on the returned angle from the signal?
- i do not currently have a physics_process set up, would i need to add that to the pointing character?
- how would i format the lerp_angle to rotate the character like this?
When you are connecting a signal you can edit the Receiver Method. For the first button you connect change the Receiver Method name to "_on_button_focus_entered". Then use the same name for all the other buttons and they will connect to the same method. Probably best to put the method in your character's script. Below where you enter the Receiver Method there is the Advanced toggle. Click that to show the advanced options. Pick Float from the Add Extra Call Arguments drop down menu and click the Add button. It will have the default value of 0 but you can change this to whatever value you want.
For the first button you connect change the Receiver Method name to "_on_button_focus_entered".
so for each of these signals they would all simply use the name "button" in the receiver method instead of individual different names for each button?
That's correct.
Yes. Then you would edit the function to be:
func _on_button_focus_entered(new_angle : float):
target_angle = new_angle
Sorry, new(ish) to Discord so struggling with code formatting!
thats alright no problem, appreciate the help
Anyway, you will need to declare the target_angle variable as a float in your character script.
right okay
What is the character in your scene, are they a Sprite2D, TextureRect, etc?
sprite2D
jakkidpoint is the name of the sprite
var target_angle: float
is that the right way to declare it?
Your script should be like this I think
var target_angle := 0.0
func _on_button_focus_entered(new_angle : float):
target_angle = new_angle
func _physics_process(delta : float) -> void:
$Sprite2D.rotation_degrees = lerp_angle($Sprite2D.rotation_degrees, target_angle, 0.3)
hmm okay
ive laid out the code on the sprite to be like that
and now ive properly connected all the button's signals to that function
looks like at the moment it's telling me
E 0:00:00:0886 jakkidpoint.gd:18 @ _physics_process(): Node not found: "Sprite2D" (relative to "/root/MainMenu/jakkidpoint").
Take out the "$Sprite2D." part, so it is just:
rotation_degrees = lerp_angle(rotation_degrees, target_angle, 0.3)
oh hey there we go
now i just gotta change the degrees for each button
i did input -85 for the new game button, though it seems to only be rotating a small amount
That line should actually be:
rotation = lerp_angle(rotation, deg_to_rad(target_angle), 0.3)
oh THERE we go
Play around with the "0.3" value to fine tune the speed. Should be somewhere in the range of 0.0 to 1.0.
whoops wrong upload
its perfect
(though yeah ill mess around with the speed like you said)
Cool, glad I could help.