Hello all,
I am working on a 3D arcade shooter project and I have ran into a bump earlier than I would have liked. I had my AnimationTree filled with Blend2s and it was working with all the values I obtained in this model. I have since moved to the BlendSpace2D and cannot seem to figure out why my animations are not playing correctly in game. The blends work well in the preview, but are not so hot in the game once compiled.
Before looking at the code (which is for a character controller), here are some need to knows:
Walking forward require the tree to be at (0, 1)
Left (-1, 0)
Right (1, 0)
Idle (0, 0)
And naturally I want them blended for a natural look --- Thanks in advance to anyone that might be able to help.
------------------- VIDEO ------------------------
https://youtube.com/watch?v=slpQYEZ6vDY
-------------------- CODE ------------------------
extends KinematicBody
#MOVEMENT VARIABLES
export var speed : float = 15
export var current_speed : float = 0
export var acceleration : float = 5
export var air_acceleration : float = 5
export var gravity : float = 0.98
export var max_terminal_velocity : float = 54
export var jump_power : float = 20
export(float, 0.1, 1) var mouse_sensitivity : float = 0.3
export(float, -90, 0) var min_pitch : float = -90
export(float, 0, 90) var max_pitch : float = 90
var velocity : Vector3
var y_velocity : float
onready var cam_pivot = $Cam_Pivot
onready var cam = $Cam_Pivot/Cam_Spring/Camera
#ANIMATION VARIABLES
onready var animation_tree = get_node("AnimationTree")
onready var animation_mode = animation_tree.get("parameters/playback")
var moving = false
var movement_state_x = 0
var movement_state_y = 0
#UI VARIABLES
var can_move = true
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _process(_delta):
#TOGGLE CONTROLLER
if can_move:
if Input.is_action_just_pressed("ui_cancel"):
can_move = false
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
if Input.is_action_just_pressed("ui_cancel"):
can_move = true
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
_handle_movement(delta)
_handle_animation(delta)
func _input(event):
#MOUSE CONTROLS
if event is InputEventMouseMotion:
if can_move:
rotation_degrees.y -= event.relative.x * mouse_sensitivity
cam_pivot.rotation_degrees.x -= event.relative.y * mouse_sensitivity
cam_pivot.rotation_degrees.x = clamp(cam_pivot.rotation_degrees.x, min_pitch, max_pitch)
#MOVEMENT
func _handle_movement(delta):
var direction = Vector3()
moving = false
movement_state_x = 0
movement_state_y = 0
#GRAVITY CALCULATION
if is_on_floor():
y_velocity = -0.01
else:
y_velocity = clamp(y_velocity - gravity, -max_terminal_velocity, max_terminal_velocity)
if can_move:
#DIRECTIONAL MOVEMENT
if Input.is_action_pressed("move_forward"):
direction -= transform.basis.z
moving = true
movement_state_y = 1
if Input.is_action_pressed("move_backward"):
direction += transform.basis.z
moving = true
movement_state_y = 1 #Change to -1 when walk_b animation is implemented
if Input.is_action_pressed("move_left"):
direction -= transform.basis.x
moving = true
movement_state_x = -1
if Input.is_action_pressed("move_right"):
direction += transform.basis.x
moving = true
movement_state_x = 1
#JUMP
if Input.is_action_just_pressed("move_jump") and is_on_floor():
y_velocity = jump_power
#CALCULATE DIRECTIONAL MOVEMENT
direction = direction.normalized()
var accel = acceleration if is_on_floor() else air_acceleration
velocity = velocity.linear_interpolate(direction * speed, accel * delta)
#CALCULATE JUMPING
velocity.y = y_velocity
#FINALIZING MOVEMENT
velocity = move_and_slide(velocity, Vector3.UP)
#``ANIMATIONS
func _handle_animation(delta):
var accel = acceleration if is_on_floor() else air_acceleration
var movement_state = Vector2(movement_state_x, movement_state_y)
if moving:
#Walk
if current_speed < speed:
current_speed = current_speed + (accel * delta * 3)
else:
current_speed = speed
movement_state_x = (current_speed * movement_state_x) / speed
movement_state_y = (current_speed * movement_state_y) / speed
movement_state = Vector2(movement_state_x, movement_state_y)
animation_tree.set("parameters/movement_state/blend_position", movement_state)
else:
#Idle
if current_speed > 0:
current_speed = current_speed - (accel * delta * 3)
else:
current_speed = 0
movement_state_x = (current_speed * movement_state_x) / speed
movement_state_y = (current_speed * movement_state_y) / speed
movement_state = Vector2(movement_state_x, movement_state_y)
animation_tree.set("parameters/movement_state/blend_position", movement_state)