#Making Graphic Rotate Based on Menu Selection

1 messages · Page 1 of 1 (latest)

sly flare
#

Making Graphic Rotate Based on Menu Selection

#

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?

viscid cairn
#

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.

sly flare
#

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

sly flare
#

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?
viscid cairn
#

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.

sly flare
#

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?

viscid cairn
#

That's correct.

sly flare
#

so like this?

viscid cairn
#

Yes. Then you would edit the function to be:

func _on_button_focus_entered(new_angle : float):
    target_angle = new_angle
sly flare
viscid cairn
#

Sorry, new(ish) to Discord so struggling with code formatting!

sly flare
#

thats alright no problem, appreciate the help

viscid cairn
#

Anyway, you will need to declare the target_angle variable as a float in your character script.

sly flare
#

right okay

viscid cairn
#

What is the character in your scene, are they a Sprite2D, TextureRect, etc?

sly flare
#

sprite2D

#

jakkidpoint is the name of the sprite

#

var target_angle: float
is that the right way to declare it?

viscid cairn
#

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)
sly flare
#

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").

viscid cairn
#

Take out the "$Sprite2D." part, so it is just:

rotation_degrees = lerp_angle(rotation_degrees, target_angle, 0.3)
sly flare
#

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

viscid cairn
#

That line should actually be:

rotation = lerp_angle(rotation, deg_to_rad(target_angle), 0.3)
sly flare
#

oh THERE we go

viscid cairn
#

Play around with the "0.3" value to fine tune the speed. Should be somewhere in the range of 0.0 to 1.0.

sly flare
#

whoops wrong upload

#

its perfect

#

(though yeah ill mess around with the speed like you said)

viscid cairn
#

Cool, glad I could help.