i want to make the player attack when i pressed fire

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

please somone help me im struggle with this one, when i pressed fire while im not moving is just stop the animation, but when i jump and while on the air i pressed fire the attack animation, help me :frowning:
heres my code:

extends KinematicBody

const MOVE_SPEED = 10
const JUMP_FORCE = 25
const GRAVITY = 0.98
const MAX_FALL_SPEED = 25
 
const H_LOOK_SENS = 0.4
 
onready var cam = $CamBase
onready var anim = $main/AnimationPlayer
 
var y_velo = 0

func _process(delta):
	anim.get_animation("Run").set_loop(true)
	if Input.is_action_pressed("quit"):
		get_tree().quit()
	if Input.is_action_pressed("restart"):
		kill()
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event):
	if event is InputEventMouseMotion:
		rotation_degrees.y -= event.relative.x * H_LOOK_SENS

func _physics_process(delta):
	var move_vec = Vector3()
	if Input.is_action_pressed("fire"):
		anim.play("Attack")
	if Input.is_action_pressed("forward"):
		move_vec.x -= 1
	if Input.is_action_pressed("backward"):
		move_vec.x += 1
	if Input.is_action_pressed("right"):
		move_vec.z -= 1
	if Input.is_action_pressed("left"):
		move_vec.z += 1
	move_vec = move_vec.normalized()
	move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation.y)
	move_vec *= MOVE_SPEED
	move_vec.y = y_velo
	move_and_slide(move_vec, Vector3(0, 1, 0))

	var grounded = is_on_floor()
	y_velo -= GRAVITY
	var just_jumped = false
	if grounded and Input.is_action_just_pressed("jump"):
		just_jumped = true
		y_velo = JUMP_FORCE
	if grounded and y_velo <= 0:
		y_velo = -0.1
	if y_velo < -MAX_FALL_SPEED:
		y_velo = -MAX_FALL_SPEED

	if just_jumped:
		play_anim("Jump")
	elif grounded:
		if move_vec.x == 0 and move_vec.z == 0:
			play_anim("Idle")

func play_anim(name):
	if anim.current_animation == name:
		return
	anim.play(name)

func kill():
	get_tree().reload_current_scene()

oh yea i forgot, below me its the code: help meeeeeeee

extends KinematicBody

const MOVE_SPEED = 10
const JUMP_FORCE = 25
const GRAVITY = 0.98
const MAX_FALL_SPEED = 25
 
const H_LOOK_SENS = 0.4
 
onready var cam = $CamBase
onready var anim = $main/AnimationPlayer
 
var y_velo = 0

func _process(delta):
	anim.get_animation("Run").set_loop(true)
	if Input.is_action_pressed("quit"):
		get_tree().quit()
	if Input.is_action_pressed("restart"):
		kill()
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event):
	if event is InputEventMouseMotion:
		rotation_degrees.y -= event.relative.x * H_LOOK_SENS

func _physics_process(delta):
	var move_vec = Vector3()
	if Input.is_action_just_pressed("fire"):
		anim.play("Idle")
	if Input.is_action_pressed("forward"):
		move_vec.x -= 1
	if Input.is_action_pressed("backward"):
		move_vec.x += 1
	if Input.is_action_pressed("right"):
		move_vec.z -= 1
	if Input.is_action_pressed("left"):
		move_vec.z += 1
	move_vec = move_vec.normalized()
	move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation.y)
	move_vec *= MOVE_SPEED
	move_vec.y = y_velo
	move_and_slide(move_vec, Vector3(0, 1, 0))

	var grounded = is_on_floor()
	y_velo -= GRAVITY
	var just_jumped = false
	if grounded and Input.is_action_just_pressed("jump"):
		just_jumped = true
		y_velo = JUMP_FORCE
	if grounded and y_velo <= 0:
		y_velo = -0.1
	if y_velo < -MAX_FALL_SPEED:
		y_velo = -MAX_FALL_SPEED

	if just_jumped:
		play_anim("Jump")
	elif grounded:
		if move_vec.x == 0 and move_vec.z == 0:
			play_anim("Idle")
		else:
			play_anim("Run")

func play_anim(name):
	if anim.current_animation == name:
		return
	anim.play(name)

func kill():
	get_tree().reload_current_scene()

gilz | 2020-06-24 09:12

oh yea i forgot, below me its the code"i forgot to copy it": help meeeeeeee

	if just_jumped:
		play_anim("Jump")
	elif grounded:
		if move_vec.x == 0 and move_vec.z == 0:
			play_anim("Idle")
		else:
			play_anim("Run")

gilz | 2020-06-24 09:14

:bust_in_silhouette: Reply From: Czselu349

Sorry, but your code is kinda spaggeti, I recon’ you these awsome tutorials about State Machines:

The other half of the tutorial: (kinda)

For controlling the player I reccommend using this method:

var move_direction = Vector3.ZERO
var speed = 600

move_direction.x = int(get_action_strenght("ui_right")) -
                   int(get_action_strenght("ui_left"))
move_direction.z = int(get_action_strenght("ui_down")) -
                   int(get_action_strenght("ui_up"))
move_direction = move_direction.normalized()
velocity = speed * move_direction
velocity = move_and_slide(velocity, Vector3(0,1,0))

In my opinion you should defenently redo the player, (before fixing the animations) because you can just save a ton of time. After makeing a state machine, it’ll be so much easyer to animate, and fire :slight_smile:

#PS: What I said is just my opinion. I’m not saying this to annoy you or make you think you can’t code, I just want to save you trouble. :wink: