I have the following code which works very well. I mean... pretty well. The only problem, is when the animation state switches, it's very edgy and sudden, and it doesn't look natural AT ALL. Does anyone know how I can fix that? Thank you in advance!!!
//this runs in Update()
if (Input.GetKey(KeyCode.Space) || jumped) {
if (!characterController.isGrounded)
jumped = true;
else
jumped = false;
if (jumped) {
animator.Play("jump");
return;
}
}
if (characterController.velocity == Vector3.zero) {
animator.Play("idle");
if (!weaponEquipped && !switchingWeapon)
animator.Play("idleArms", 1);
return;
} else if (Input.GetKey("a")) {
if (isRunning && !vitals.isEmpty("stamina")) {
animator.Play("runLeft");
return;
}
animator.Play("walkLeft");
return;
}
if (Input.GetKey("d")) {
if (isRunning && !vitals.isEmpty("stamina")) {
animator.Play("runRight");
return;
}
animator.Play("walkRight");
return;
} else if (Input.GetKey("s")) {
if (isRunning && !vitals.isEmpty("stamina")) {
animator.Play("runBack");
if (!weaponEquipped && !switchingWeapon)
animator.Play("runBackArms", 1);
return;
}
animator.Play("walkBack");
if (!weaponEquipped && !switchingWeapon)
animator.Play("walkBackArms", 1);
return;
} else if (isRunning && !vitals.isEmpty("stamina")) {
animator.Play("run");
if (!weaponEquipped && !switchingWeapon)
animator.Play("runArms", 1);
return;
} else {
animator.Play("walk");
if (!weaponEquipped && !switchingWeapon)
animator.Play("walkArms", 1);
} if (!isRunning && characterController.velocity.magnitude > 0) {
animator.Play("walk");
return;
}
What you're looking for is a Blend Tree, which, combined with Animator, smoothly fades between animations, based on the variable that you define. I can't rewrite your whole code and setup your Blend Tree, but here's a starting point:
using UnityEngine;
using System.Collections;
using System.Collections.Generics;
public class YourClass: MonoBehaviour
{
public Animator animator;
float vertical = 0f;
float horizontal = 0f;
void Update()
{
if (Input.GetButtonDown("Jump"))
{
animator.SetTrigger("Jump");
}
else
{
vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal");
animator.setFloat("Vertical Movement", vertical);
animator.setFloat("Horizontal Movement", horizontal);
}
}
}