- Edited
SOLVED:
When I made the post I was using different code for player movement and attack, I switched to using animationTrees and learned of the position.direction.to_(get_global_mouse_position) code!
if(Input.is_action_just_pressed("Attack")): #input for attacking, the position of the mouse is then acquired and placed on the attackDirection var
attackDirection = position.direction_to(get_global_mouse_position()) #gets your mouse position relative to the player
animation_tree["parameters/conditions/attack"] = true
animation_tree["parameters/Attack/blend_position"] = attackDirection #Plays the attack aniamtion blend position with the blend position value from the attackDirection
else:
animation_tree["parameters/conditions/attack"] = false #no longer attacking
if(moveDirection != Vector2.ZERO):
animation_tree["parameters/Idle/blend_position"] = moveDirection
animation_tree["parameters/Walk/blend_position"] = moveDirection
Hi!
I'm working on a top down pixel action RPG and I'm entirely new to godot and coding.
The controls I'm trying to work on are as follows:
- Player sprite moves and faces camera / 4 directional movement
- Player sprite attacks in the direction of camera (example control: welcome to the gungeon), it's not just one attack but with combos and skills in the future.
- If player is moving left but mouse is on the right side: Player sprite that's on walking / running animation should still face mouse.
- If player dodge rolls, they dodge in the direction they're moving, not the camera face. (not in the code yet)
What I want to do:
- Have Player Sprite play the appropriate directional idle / walk when mouse is at a certain point to "face it" - this comes alongside the attacks.
EDIT: New idea: Have Player Sprite instead play the appropriate directional attack if I were to keep it simple, just keeping it in here.
Q: "How will I make it so that the player sprite faces the same direction of the mouse cursor, and attack in the same direction you left click?"
The details of what I've done so far:
- AnimatedSprite2D for the sprite and AnimationPlayer for animations.
- AnimationPlayer contains 12 different animations: 4 directional idles , 4 directional basic attack and 4 directional walks. They're named Idle, Attack and Walk but with the initial of the direction with underscore (example: L_Walk for left walk)
This is old code:
Click to reveal Click to hide
extends CharacterBody2D
class_name PlayerMain
var basespeed: int = 200 #speed
var previousDirection = "R_" #Variable for storing previousDirection
var direction = "R_" #CurrentDirection
var mouse_direction: Vector2 = (get_global_mouse_position() - global_position).normalized() #attempt for mouse direction
var AttackIP: bool = false #Attack InProgress
@onready var animation = $PlayerAnims
#PLAYER MOVEMENT CODE
func handleInput():
var moveDirection = Input.get_vector("Move_Left","Move_Right", "Move_Up", "Move_Down")
velocity = moveDirection * basespeed
if Input.is_action_just_pressed("Attack"):
animation.play(previousDirection + "Attack")
AttackIP = true
await animation.animation_finished
AttackIP = false
func updateAnimation():
if AttackIP: return
if velocity.x < 0: direction = "L_";previousDirection = "L_" #Means if going left, direction becomes L
elif velocity.y < 0: direction = "U_";previousDirection = "U_" #Up, direction becomes U
elif velocity.y > 0: direction = "D_";previousDirection = "D_" #Down, direction becomes D
elif velocity.x > 0: direction = "R_";;previousDirection = "R_" #Right, direction becomes R
if velocity.length() == 0: #Stop
animation.play(previousDirection + "Idle") #The last direction acquired from the else call
else:
animation.play(direction + "Walk") #PlayerAnims has the animation timeline name
#func mousemove():
# if mouse_direction.x > 0 and animation.current_animation == "L_Idle" and velocity.length() == 0:
# animation.play("R_Idle")
# elif mouse_direction.x < 0 and not animation.current_animation == "L_Idle" and velocity.length() == 0:
# animation.play("L_Idle")
func _physics_process(_delta):
handleInput()
move_and_slide()
updateAnimation()
This is the Player Scene Tree:
The func mousemove that's in comments was a test to try and use mouse.direction to see if player will look left and right, it did not work but I thought to keep it in here in case I learn something about it.
I also apologize for the excess comments as they're there for me to really get it in my head to understand. (I'm a slow visual learner)
For further info that may help I also followed this tutorial: Tutorial by Maker Tech