- Home /
How to fix multiple character animations playing at the same time during a run animation?
I have imported my character model from Blender into Unity, with the animations functioning properly in the preview screen. I followed many tutorial videos about how these animations could be assigned to different character movements, however I encountered a problem. My "Idle" and "Jump" animations work fine, but my "Run" animation glitches and plays the run and jump animations at the same time, as seen in the attached clip. I have assigned two parameters to my animations, and set these to my game in my code:
speed - (float, set at 0.0)
grounded - (bool, set unticked)
P.S. The name of my character model is "solidified body and fixed belt character"
P.S. I have mapped all animation transitions in accordance with multiple videos and i'm pretty sure they are correct. For example, the "Run" to "Jump" transition has grounded set to false, and speed set to greater than 0.1 (as seen in the attached image).
If anyone has any suggestions it would be much appreciated. I'm new to Unity so it would also help if you could give as much detail in your answers as possible. Thanks :)
Link to video of glitching character: https://youtu.be/D-bgSnLvU14
CODE:
public class Capsule : MonoBehaviour
{
public CharacterController _controller;
public Transform cam;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
public Transform GroundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
public Rigidbody rb;
public Animator anim;
// Start is called before the first frame update
void Start()
{
_controller = GetComponent<CharacterController>();
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(h, 0, v).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0, angle, 0);
Vector3 moveDir = Quaternion.Euler(0, targetAngle, 0) * Vector3.forward;
_controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
anim.SetFloat("speed", Mathf.Abs(h+v));
anim.SetBool("grounded", _controller.isGrounded);
velocity.y += gravity * Time.deltaTime;
_controller.Move(velocity * Time.deltaTime);
}
[1]: /storage/temp/174111-animator-controller.png
Answer by jprice2 · Jan 14, 2021 at 04:49 AM
Could it be because my animation type is "Generic" not "Humanoid"?