I'm starting with the animations,
I have a character with movement animations for right, left and up
When uploading, the character performs the animation correctly, but on the console I get an error: "play: Animation not found: Run_Up"
error :"play: Animation not found: Idle_Up"
the error starts to come out, when I'm going up.
it stops when i go anywhere else
Error log
Error detail
Animations list
Scene
extends KinematicBody2D
var VELOCIDAD_MAXIMA = 100
var ACELERACION = 350
var FRICCION = 850
enum {
RUN,
PUSH
}
onready var animation_player = $AnimationPlayer
export var velocidad = Vector2.ZERO#(0, 0)
export var direccion_vector = Vector2.DOWN#(0, 1)
export var direccion = Vector2.ZERO#(0, 0)
export var push_finished = false
var estado = RUN
var targets = []
func run_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:
direccion_vector = input_vector
velocidad = velocidad.move_toward(input_vector * VELOCIDAD_MAXIMA, ACELERACION * delta)
_play_animation("Run")
#Si el usuario no esta tocando al personaje
elif velocidad.length() == 0 && Input.is_action_pressed("action"):
_play_animation("Push")
if $RayCast2D.is_colliding():
push_finished = false
estado = PUSH
else:
velocidad = velocidad.move_toward(input_vector * VELOCIDAD_MAXIMA, FRICCION * delta)
_play_animation("Idle")
velocidad = move_and_slide(velocidad)
func push_state(_delta):
var collider = $RayCast2D.get_collider()
collider._on_interact($RayCast2D.rotation_degrees)
if collider:
estado = RUN
velocidad = move_and_slide(velocidad)
func animation_finished():
if !Input.is_action_pressed("action"):
estado = RUN
velocidad = Vector2.ZERO
func _physics_process(delta):
match estado:
RUN:
run_state(delta)
PUSH:
push_state(delta)
func _play_animation(animation_type: String) -> void:
var animation_name = animation_type + "_" + _get_direction_string(direccion_vector.angle())
if animation_name != animation_player.current_animation:
animation_player.stop(true)
animation_player.play(animation_name)
func _get_direction_string(angle: float) -> String:
var angle_deg = round(rad2deg(angle))
#glanced up
if angle_deg == -90:
$RayCast2D.rotation_degrees = 180
return "Up"
#glanced down
if angle_deg == 90:
$RayCast2D.rotation_degrees = 0
return "Right"
#glanced left
if angle_deg == 180:
$RayCast2D.rotation_degrees = 90
return "Left"
#glanced right
if angle_deg == 0:
$RayCast2D.rotation_degrees = -90
return "Right"
return "Right"
I was able to fix the error
apparently there was another character outside the scene that was causing conflict, since he had other types of animations
I deleted that character and it was solved
Related
im making my first 3d game for a jam and i was writing some code about the player state machine, it was all working, but when i put this code in the state_run(delta) func, it crashes:
if playerIsMoving == false:
initialize_idle()
it gives me the error invalid set intex z (base int) with value type of float.
Code:
extends KinematicBody
var sensitivity = 0.06
var speed = 10
var h_aceleration = 6
var gravity = 20
var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var grav_vec = Vector3()
onready var head = $Head
enum STATES { IDLE, RUN, ATTACK, DEAD}
var state_cur : int
var state_nxt : int
var test = Vector3.ZERO
var playerIsMoving = false
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
state_nxt = STATES.IDLE
func _input(event):
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x * sensitivity))
head.rotate_x(deg2rad(-event.relative.y * sensitivity))
head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))
func _physics_process(delta):
if state_nxt != state_cur:
state_cur = state_nxt
match state_cur:
STATES.IDLE:
state_idle(delta)
STATES.RUN:
state_run(delta)
STATES.ATTACK:
pass
STATES.DEAD:
pass
print(state_cur)
if Input.is_action_pressed("forward") or Input.is_action_pressed("backwards") or Input.is_action_pressed("left") or Input.is_action_pressed("right"):
playerIsMoving = true
else:
playerIsMoving = false
if not is_on_floor():
grav_vec += Vector3.DOWN * gravity * delta
else:
grav_vec = -get_floor_normal() * gravity
func initialize_idle():
state_nxt = STATES.IDLE
movement = 0
func state_idle(delta):
if Input.is_action_pressed("left") or Input.is_action_pressed("right") or Input.is_action_pressed("forward") or Input.is_action_pressed("backwards"):
initialize_run()
func initialize_run():
state_nxt = STATES.RUN
pass
func state_run(delta):
direction = Vector3()
if Input.is_action_pressed("forward"):
direction -= transform.basis.z
elif Input.is_action_pressed("backwards"):
direction += transform.basis.z
elif Input.is_action_pressed("left"):
direction -= transform.basis.x
elif Input.is_action_pressed("right"):
direction += transform.basis.x
else:
playerIsMoving = false
direction = direction.normalized()
h_velocity = h_velocity.linear_interpolate(direction * speed, h_aceleration * delta)
movement.z = h_velocity.z + grav_vec.z
movement.x = h_velocity.x + grav_vec.x
movement.y = grav_vec.y
move_and_slide(movement, Vector3.UP)
if playerIsMoving == false:
initialize_idle()
I alredy tried to round the value or use the int() but it isnt working, the code is giving error on the "movement.z = h_velocity.z + grav_vec.z"
Types.
Look, movement is a Variant, initialized to a Vector3:
var movement = Vector3()
And here you use it as a Vector3:
movement.z = h_velocity.z + grav_vec.z
movement.x = h_velocity.x + grav_vec.x
movement.y = grav_vec.y
move_and_slide(movement, Vector3.UP)
But here 0 is an int not a Vector3:
movement = 0
So after that movement... Continues to be a Variant, but now it has an int value. So when you use it as a Vector3 it fails. Because an int does not have x, y, z.
You can declare movement to be a Vector3 explicitly:
var movement:Vector3 = Vector3()
Or implicitly (inferred from the value you assigned):
var movement:= Vector3()
And then Godot will tell you that this line:
movement = 0
Is an error, because you are trying to set an int to a Vector3.
My guess is that you want to set movement to the zero vector, which is like this:
movement = Vector3(0.0, 0.0, 0.0)
Or like this:
movement = Vector3.ZERO
Or - similarly to how you initialized it - like this:
movement = Vector3()
im trying to make a 3d fighting game, when i start it the player can move around like normal, but after a few seconds the player just freezes, the animations work fine, but the player will just freeze in place.
i looked through the code a bunch of times but i cant find what's causing it, plz help
Code:
extends KinematicBody
onready var anim = $PlayerANIM/AnimationPlayer
export var speed = 10
const ACCEL = 15.0
const AIR_ACCEL = 9.0
const JUMP_SPEED = 15
var velocity = Vector3.ZERO
var velocity_info = Vector3.ZERO
var current_vel = Vector3.ZERO
var snap = Vector3.ZERO
var gravity = -40
var can_run = true
var dir = Vector3.ZERO
func play_anim(dir):
if anim.is_playing() == false:
anim.play("IDLE")
if Input.is_action_just_pressed("a") or Input.is_action_just_pressed("d") and can_run:
anim.stop()
anim.play("RUN")
if Input.is_action_just_released("a") or Input.is_action_just_released("d") and can_run:
anim.stop()
func _physics_process(delta):
#MOVEMENT
dir = Vector3.ZERO
if Input.is_action_pressed("d") and can_run:
rotation_degrees = Vector3(0,0,0)
if anim.is_playing() == false:
anim.play("RUN")
dir += global_transform.basis.x
if Input.is_action_pressed("a") and can_run:
rotation_degrees = Vector3(0,180,0)
if anim.is_playing() == false:
anim.play("RUN")
dir += global_transform.basis.x
if Input.is_action_just_pressed("Punch") and $PunchTimer.is_stopped():
anim.stop()
anim.play("PUNCH")
$PunchTimer.start()
can_run = false
if Input.is_action_just_pressed("Kick") and $KickTimer.is_stopped():
anim.stop()
anim.play("KICK")
$KickTimer.start()
can_run = false
if Input.is_action_just_released("a") or Input.is_action_just_released("d"):
anim.stop()
if Input.is_action_just_pressed("space") and is_on_floor():
#velocity.y = 15
anim.play("ROLL")
$RollTimer.start()
var target_vel = dir * speed
var accel = ACCEL if is_on_floor() else AIR_ACCEL
current_vel = current_vel.linear_interpolate(target_vel, accel * delta)
velocity.x = current_vel.x
velocity.z = current_vel.z
velocity.y += gravity *delta
move_and_slide_with_snap(velocity, snap, Vector3.UP, true, 4, deg2rad(45))
play_anim(dir)
func _on_PunchTimer_timeout():
can_run = true
func _on_KickTimer_timeout():
can_run = true
func _on_RollTimer_timeout():
pass
I guess you need to change Input.is_action_just_pressed("action_name") to Input_is_actions_pressed("action_name").
The difference is that first functions returns 'true' only once while you pressing the button (which is good, for example, when you make pistol shooting), and second one returns 'true' all the time you pressing the button.
I have a point and click game for which I'm adapting joypad controls. I want the mouse to move with thumbsticks and trigger mouse click with joystick A button.
I can move the mouse using get_viewport().warp_mouse(mouse_pos)
I can trigger mouse click creating new mousebuttonevent and parsing it
var left_click = InputEventMouseButton.new()
left_click.pressed = pressed
left_click.button_index = button_index
left_click.position = get_viewport().get_mouse_position()
print("emulated: ", left_click.as_text())
Input.parse_input_event(left_click)
But after I call Input.parse_input_event(left_click) and move thumsticks again it has reset the mouse position to 0, 0.
Here is the full code. I'm quite confused.
var mouse_pos
func _ready():
$ColorRect/Button.connect("pressed", self, "pressed")
func pressed():
print("button pressed")
func _input(event):
if event is InputEventMouseButton:
print("from _input: ", event.as_text())
elif event is InputEventJoypadButton:
if event.is_action_pressed("a_button"):
print("pressed A")
emulate_click(BUTTON_LEFT , true)
# elif event.is_action_released("ui_accept"):
# print("released A")
# emulate_click(BUTTON_LEFT, false)
func emulate_click(button_index, pressed):
var left_click = InputEventMouseButton.new()
left_click.pressed = pressed
left_click.button_index = button_index
left_click.position = get_viewport().get_mouse_position()
print("emulated: ", left_click.as_text())
Input.parse_input_event(left_click)
# get_viewport().warp_mouse(cached_mouse_pos)
# pointer.global_position = previous_mouse_pos
func _process(delta):
var direction: Vector2
direction.x = Input.get_action_strength ("t_right", true) - Input.get_action_strength ("t_left", true)
direction.y = Input.get_action_strength ("t_down", true) - Input.get_action_strength ("t_up", true)
if abs(direction.x) == 1 and abs(direction.y) == 1:
direction = direction.normalized()
var thumbstick_sensitivity = 1000
var movement = thumbstick_sensitivity * direction * delta
if (movement):
mouse_pos = get_viewport().get_mouse_position() + movement
print(mouse_pos)
get_viewport().warp_mouse(mouse_pos)```
Any ideas are super welcome!
You need to set the global position (global_pos) of the new InputEventMouseButton object.
This is due to the L332-L344 of input_default.cpp in Godot's source code, which read:
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid()) {
if (mb->is_pressed()) {
mouse_button_mask |= (1 << (mb->get_button_index() - 1));
} else {
mouse_button_mask &= ~(1 << (mb->get_button_index() - 1));
}
Point2 pos = mb->get_global_position();
if (mouse_pos != pos) {
set_mouse_position(pos);
}
If you don't set the global position, it defaults to (0,0) which is why you're seeing this unexpected behaviour.
I am new to Godot, and I am creating my first project. There's something that annoys the heck out of me. It's this: "The function 'connect()' returns a value, but this value is never used." I don't know what to add to the line of code, but I will first show the code:
extends KinematicBody2D
var gravity = 1000
var velocity = Vector2.ZERO
var maxHorizontalSpeed = 120
var horizontalAcceleration = 100
var jumpSpeed = 320
var jumpTerminationMultiplier = 4
var hasDoubleJump = false
func _ready():
warning-ignore:return_value_discarded
$HazardArea.connect("area_entered", self, "on_hazard_area_entered")
func _process(delta):
var moveVector = Vector2.ZERO
moveVector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
moveVector.y = -1 if Input.is_action_just_pressed("jump") else 0
velocity.x += moveVector.x * horizontalAcceleration * delta
if (moveVector.x == 0):
velocity.x = lerp(0, velocity.x, pow(2, -50))
velocity.x = clamp(velocity.x, -maxHorizontalSpeed, maxHorizontalSpeed)
if (moveVector.y < 0 && (is_on_floor() || $CoyoteTimer.is_stopped() || hasDoubleJump)):
velocity.y = moveVector.y * jumpSpeed
if(!is_on_floor() && $CoyoteTimer.is_stopped()):
hasDoubleJump = false
$CoyoteTimer.stop()
if(moveVector.y < 0 && !Input.is_action_just_pressed("jump")):
velocity.x = gravity * jumpTerminationMultiplier * delta
else:
velocity.y += gravity * delta
var wasOnFloor = is_on_floor()
velocity = move_and_slide(velocity, Vector2.UP)
if(wasOnFloor && !is_on_floor()):
$CoyoteTimer.start()
if(is_on_floor()):
hasDoubleJump = true
update_animation()
func get_movement_vector():
var moveVector = Vector2.ZERO
moveVector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
moveVector.y = -1 if Input.is_action_just_pressed("jump") else 0
return moveVector
func update_animation():
var moveVec = get_movement_vector()
if(!is_on_floor()):
$AnimatedSprite.play("Jump")
elif(moveVec.x != 0):
$AnimatedSprite.play("Run")
else:
$AnimatedSprite.play("Idle")
$AnimatedSprite.flip_h = true if moveVec.x > 0 else false
func on_hazard_area_entered(_area2d):
print("die")
The problem is on line 13, and I would really appreciate some help!
there is not a solution for this problem. The only thinks you can do are these:
Or use the returning value (if you want)
Or press ignore in warning tab
This link can help you more https://godotengine.org/qa/78337/the-function-connect-returns-value-but-this-value-never-used
# changed switch statement setup by creating a new variable and using "." operator
# removed extra delta in move_and_slide function
# left off attempting to add gravity to the game
# 2/26/2019
# removed FSM and will replace it with tutorial video code, for sake of completion
extends Node2D
const FLOOR = Vector2(0,-1)
const GRAVITY = 5
const DESCEND = 0.6
var speed = 100
var jump_height = -250
var motion = Vector2()
var jump_count = 0
var currentState = PlayerStates.STATE_RUNNING
var grounded = false
enum PlayerStates {STATE_RUNNING, STATE_JUMPING, STATE_DOUBLE_JUMPING, STATE_GLIDING}
func _ready():
var currentState = PlayerStates.STATE_RUNNING
pass
func jump():
motion.y = jump_height
func glide():
if motion.y < 500:
motion.y += DESCEND
func _process(delta):
var jump_pressed = Input.is_action_pressed('jump')
var glide_pressed = Input.is_action_pressed('glide')
* the code below is where I attempted to count the jumps in order to keep them from surpassing two jumps. My goal is to create a double
jump and so I used the less than operator to control that number*
if jump_pressed:
if jump_count < 2:
jump_count += 1
jump()
grounded = false <-- I had to copy paste this code again, below, so I don't get an error in my question.
if jump_pressed:
if jump_count < 2:
jump_count += 1
jump()
grounded = false
if grounded == false:
if glide_pressed:
glide()
motion.x = speed
motion.y += GRAVITY
motion = move_and_slide(motion, FLOOR)
if is_on_floor():
grounded = true
jump_count = 0
else:
grounded = false
First of all, I think you need to use a KinematicBody2D and perform your logic in _physics_process if you would like to use move_and_slide, other than that, your code was almost working:
extends KinematicBody2D
const FLOOR = Vector2(0,-1)
const GRAVITY = 3000
const DESCEND = 0.6
var speed = 100
var jump_height = 250
var motion = Vector2()
var jump_count = 0
func jump():
motion.y = -jump_height
func glide():
if motion.y < 500:
motion.y += DESCEND
func _physics_process(delta):
var jump_pressed = Input.is_action_pressed('jump')
var glide_pressed = Input.is_action_pressed('glide')
motion.y += delta * GRAVITY
var target_speed = Vector2(speed, motion.y)
if is_on_floor():
jump_count = 0
if glide_pressed:
glide()
if jump_pressed and jump_count < 2:
jump_count += 1
jump()
motion = lerp(motion, target_speed, 0.1)
motion = move_and_slide(motion, FLOOR)
You also need to change the node type to KinematicBody2D in the Godot editor (right click on the node and then on change type).