0 votes

Hey, my character moves on the keys wasd, but I wanted to implement a system for him to look at the mouse cursor as well and his attacks follow where the cursor apoint.
It has some animations like idle (left, righ, top, down) and run (left, right, top, down).
The code:
`func physicsprocess(delta):
match state:
MOVE:
movestate(delta)
ATTACK:
attack
state(delta)
func ready():
mouse
pos = getlocalmouseposition()
func move
state(delta):

var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
    animatedSprite.set("parameters/Idle/blend_position",input_vector)
    animationTree.set("parameters/Idle/blend_position",input_vector)
    animationTree.set("parameters/Run/blend_position",input_vector)
    animationTree.set("parameters/Attack/blend_position",input_vector)
    animationState.travel("Run")
    velocity = velocity.move_toward(input_vector * MAX_SPD, ACCELERATION * delta)
else:
    animationState.travel("Idle")
    velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)


velocity = move_and_slide(velocity)




if Input.is_action_just_pressed("attack"):
    state = ATTACK

func attackstate(delta):
animationState.travel("Attack")
func attack
animation_fineshed():
state = MOVE `

in Projects by (14 points)

1 Answer

0 votes

Hi,
like so ... ?

player.look_at( mouseposition )

read about it here ...

https://docs.godotengine.org/en/stable/tutorials/2d/2d_movement.html#rotation-movement-mouse

by (2,430 points)

but if i do this, the sprite rotates, i want to change de sprite animation(mouse on left -> idle left)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories