how do i get a rotation value between 3 and -3 in godot

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

In previous games my rotation value used to be between -3 and 3 and thus this code use to work for manipulating the texture. Now however when i rotate the node the rotation value keeps on adding up

			 #Player ROTATION
	rotation = velocity.angle()

	if rotation <= -1.8:
		Player.flip_v = false

	elif rotation >= 1.8:
		Player.flip_v = false

	else : 
		Player.flip_v = true

Did you want it to wrap around those values, just like using the wrapf() function?

Ertain | 2022-02-16 21:15

:bust_in_silhouette: Reply From: rossunger

rotation = fmod(velocity.angle() , 3)
Fmod gives you the remainder when you divide by 3

Sorry about that my velocity is zero. I use Mouse position to rotate the node

if Input.is_action_pressed("ui_left_click"):
	Mouse_Position = get_local_mouse_position()
	rotation += Mouse_Position.angle()

javrocks | 2022-02-16 16:03

Same deal, just replace the angle with whatever you actually use :slight_smile:

rossunger | 2022-02-16 16:19

I did i replaced velocity with that line of code. But it tells me that angle() in base null

rotation = fmod(Mouse_Position.angle() , 3)

javrocks | 2022-02-16 16:22

That error means that Mouse_Position doesn’t exist… Is null.

rossunger | 2022-02-16 16:36

Also, is there a reason youre using 3? And not PI?

rossunger | 2022-02-16 16:38

The node rotates fine to my mouse position and

rotation = fmod(velocity.angle() , 3)

is the first code u told me to use (should i change 3 to PI?)

javrocks | 2022-02-16 16:46

I’m not sure what you’re trying to achieve, but a circle is 2 * PI radeons, so if you’re trying to make your object rotate around 360degrees, you should probably use -PI to +PI as your constraint, not -3 to +3 … Replacing 3 with PI would achieve that.

rossunger | 2022-02-16 16:51

I just use this and it still tells me that angle() is in base null

rotation = fmod(Mouse_Position.angle() , PI)

javrocks | 2022-02-16 16:56

You need to figure out why Mouse_Position is null.
You’ll have to do some debugging. Where are you setting it? Is it being set correctly? Is this function being called before the variable is initialised?

If its the last way then you could solve it by
var Mouse_Position = Vector2(0,0) when you declare the variable

rossunger | 2022-02-16 17:00


           -3                   -0
            3                    0    


Imagine that that thing above is a clock and as the node rotated clockwise or anticlockwise it used to output those values. That’s what i want to happen

javrocks | 2022-02-16 17:04

Oh yeh i declared it properly and now it doesn’t give me the error. But it’s still bugging and doesn’t output the right thing. This is the code

extends KinematicBody2D

var Mouse_Position = Vector2(0,0)

func _input(_event: InputEvent) -> void:

    if Input.is_action_pressed("ui_left_click"):
	    Mouse_Position = get_local_mouse_position()
	    rotation += Mouse_Position.angle()
func _physics_process(delta):

    rotation = fmod(Mouse_Position.angle() , PI)

    print (rotation)

javrocks | 2022-02-16 17:09

Maybe I misunderstood what you’re trying to do…

Do you just want to clamp the rotation so that is it’s between -3 and 3? You don’t want it to be able to rotate the whole way around?

Try `rotation = min( 3, max(-3, Mouse_Position.angle()))

rossunger | 2022-02-16 17:21

It’s glitching out
It’s spamming Zeros and -0’s in the output

javrocks | 2022-02-16 17:29