0 votes

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 :(
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()
in Engine by (16 points)

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()

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")

1 Answer

0 votes

Sorry, but your code is kinda spaggeti, I recon' you these awsome tutorials about State Machines:
https://www.youtube.com/watch?v=BNU8xNRk_oU&t=8s
The other half of the tutorial: (kinda)
https://www.youtube.com/watch?v=j_pM3CiQwts

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 :)

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. ;)

by (137 points)
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 Frequently asked questions and 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 [email protected] with your username.