Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | veiran2010 |
sorry for my english and bad code formatting.
Hey, I started a new project and I’m stuck in the character’s movement, the player can move in all directions using the wasd keys, but I want to implement a system that the character and his attacks follow the mouse cursor.
the code:
extends KinematicBody2D
const ACCELERATION = 1000
const MAX_SPD = 400
const FRICTION = 500
var rotation_speed = 20
var max_rotation = 0.1
var max_speed = 125
var rotating = false
var last_x = 0
onready var viewport = get_viewport()
var velocity = Vector2.ZERO
var state = MOVE
onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animatedSprite = $AnimatedSprite
onready var animationState = animationTree.get(“parameters/playback”)
onready var mouse_pos = null
enum{
MOVE,
ATTACK
}
func _input(event):
if event is InputEventMouseButton:
last_x = event.position.x
rotating = event.pressed
func _physics_process(delta):
match state:
MOVE:
move_state(delta)
ATTACK:
attack_state(delta)
func _ready():
mouse_pos = get_local_mouse_position()
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 attack_state(delta):
animationState.travel(“Attack”)
func attack_animation_fineshed():
state = MOVE
And your question is… ?
Also please make sure that your code is formatted properly (each line has to start with 4 spaces, each additional 4 spaces equal an indentation layer). If you don’t, underscores are interpreted as markdown and indentation is lost, which makes your script a lot harder to read.
njamster | 2020-08-26 11:07