My player character vibrates in his idle pose for some reason. He has 3 animations (IdleState, WalkState, RunState) and the latter 2 run smoothly, but when I don't provide directional inputs and he enters his idle state, he looks like he had 100 espresso shots!
43 second video demo here: https://youtu.be/QuHPrkjx-T4
I am not using a blend tree for the moment and the animation used for IdleState was exported with only a single frame (frame 1) from Blender. Unity's FBX import panel has the animation listed as playing from frame 0 to 1. I have tried many different configurations for this animation in the importer, including looping, not looping, matching pose, setting animation length to 0. None of that fixed the jitters (the latter nullified the animation). I have restructured my scripts but have not found a solution for this yet.
My character uses 2 scripts, one to control movement, and another to control animation.
This one controls movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private float charSpeed;
public float walkSpeed;
private float runSpeed;
public float jumpForce;
public CharacterController controller;
public float gravityScale;
private Rigidbody rb;
public static Vector3 moveDirection;
// Start is called before the first frame update
void Start() {
controller = GetComponent<CharacterController>();
rb = GetComponent<Rigidbody>();
charSpeed = walkSpeed;
runSpeed = walkSpeed * 2;
}
// Update is called once per frame
void Update() {
if (Input.GetButton("Fire3"))
{
charSpeed = runSpeed;
}
else {
charSpeed = walkSpeed;
}
// Declare movement vector
moveDirection = new Vector3(Input.GetAxis("Horizontal") * charSpeed,
moveDirection.y,
Input.GetAxis("Vertical") * charSpeed);
// Set y-axis movement conditions (jumping, grounded, free-fall)
if (Input.GetButton("Jump") && controller.isGrounded){
moveDirection.y = jumpForce;
}
else if (!controller.isGrounded)
{
moveDirection.y = moveDirection.y + Physics.gravity.y * gravityScale * Time.deltaTime;
}
else
{
moveDirection.y = 0;
}
// rotated 45 deg to align controls to camera instead of world space:
Vector3 rotated = Quaternion.Euler(0,-45,0) * moveDirection;
controller.Move(rotated * Time.deltaTime);
Vector3 lookDir = new Vector3(rotated.x, 0, rotated.z);
if (lookDir != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(lookDir);
}
}
}
...and this one plays the animations:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class CharacterAnimator : MonoBehaviour
{
private Rigidbody rb;
Animator animator;
void Start()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
void FixedUpdate() // was Update(), no change
{
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
{
animator.Play("IdleState");
}
else
{
if (Input.GetButton("Fire3"))
{
animator.Play("RunState");
}
else
{
animator.Play("WalkState");
}
}
}
}
What is causing this jittery idle state bug?
update
and the animation infixedUpdate
? Can this cause weirdness? What is the purpose of the while in the animation component (instead of an if)? \$\endgroup\$