#Jitttery movement of player when looking at mouse

1 messages · Page 1 of 1 (latest)

formal sage
#

so im very new to godot and just started to make my first game
i implemented a double jump system
then i wanted the character to face the mouse cursor whenever it was in the air
so did the following



const SPEED = 300.0
const JUMP_VELOCITY = -700.0
const NUM_EXTRA_JUMPS = 1

var extra_jumps : int = NUM_EXTRA_JUMPS

@onready var animation_player: AnimationPlayer = $AnimationPlayer

func _physics_process(delta: float) -> void:

    if not is_on_floor():
        velocity += get_gravity() * delta
        

        look_at(get_global_mouse_position())
        rotation += 90
    else:
        rotation = 0


    if Input.is_action_just_pressed("jump"):
        if is_on_floor():
            velocity.y = JUMP_VELOCITY
            animation_player.play("jump")
            extra_jumps = NUM_EXTRA_JUMPS
        elif extra_jumps > 0:
            velocity.y = JUMP_VELOCITY
            animation_player.play("jump")
            extra_jumps -= 1


    var direction := Input.get_axis("move_left", "move_right")
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)

    move_and_slide()```
but whenever i try to play the game and hit the floor at any other angle except straight down
it starts jittering between the mouse direction and straight up
for context i want it to point straight up when its on ground
and follow mouse direction when in air
i cant figure out what is wrong here i would be grateful if someone could point me in the right direction
#

Jitttery movement of player when looking at mouse

dim talon
#

Some quick thoughts I have that may help....

  1. You're instantly looking at the cursor. Lerping the look_at would help make it smoother if that is something you want as well.

  2. The player is looking at the cursor in the jump but you are also setting the rotation to increase by its value + 90. 2 problems here.
    a) Rotation is in radians not degrees so if you want to use 90 degrees you need to use deg_to_rad to convert it.
    b) this will cause a "jitter" when jumping as it both (i) tries to set the player to look at the mouse, (ii) proceeds to rotate the player - but with this being in the physics_process you'll find it's happening very fast. It will do this: look at mouse, rotate, look at mouse, rotate, look at mouse, rotate... etc, at many times per second. |

As well, you have direction being set to control velocity but how are you not checking that the player is on the floor or setting their rotation so that they are "standing straight" while on the floor so that they can move in the expected direction.

#

Are you following a tutorial///have you? If not you may benefit from doing one to get a good grasp on the ideas behind movement and if you have maybe look at what you did in that or other examples of people doing similar things and seeing how your approach is different from theres

lusty beacon
#

instead of converting degrees to radians you can also just use rotation_degrees instead of rotation

formal sage
#

thx for the suggestions

formal sage
#

and how can there be a jitter

#

the jitter is not 90 degrees everytime

#

its just from 0 degrees to wherever your mouse is pointing

#

so it means the is on floor function is hallucinating right?

#

i dont understand any of this 😭

lusty beacon
#

hmmm maybe just rotate the sprite instead of the whole characterbody?

#

it's a shot in the dark but it might work

dim talon
#

Which is roughly 5100 degrees of rotation. So I'd expect rotating an object dozens of times would probably look strange, fixing this may be your first route to see affect.

#

90 degrees is π/2 to convert to radians, you can and should use the unit circle often until you memorize it OR remember to always call a radians to degrees function anytime you use degrees.

#

So for 90 degrees of rotation you should specify 1.57 radians not 90 as you currently do.

formal sage
#

Still no change