Problem With Character Animations
Hello all. I'm having an issue regarding animations. I am using two scripts on the character, one that allows me to move left and right and adjust the speed, and another that triggers which animation to play when pressing either A or D (A triggering the running left animation, D triggering the running right animation). Its working fine, for the most part but i have a problem where sometimes the animation doesn't play and the character reverts to their idle animation. Another problem is sometimes the character seems to get stuck in their running animation even after letting go of A or D. I've provided video as well as the scripts below.
This one allows me to move left and right
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class script_runa_control : MonoBehaviour {
public float runSpeed;
Rigidbody myRB;
Animator myAnim;
bool facingRight;
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody>();
myAnim = GetComponent<Animator>();
facingRight = true;
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
float move = Input.GetAxis("Horizontal");
myRB.velocity = new Vector3(move * runSpeed, myRB.velocity.y, 0);
}
}
This one triggers the animations depending on which key is pressed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class script_runa_triggerrunninganim : MonoBehaviour {
public Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("d"))
{
anim.Play("anim_runa_run");
}
if (Input.GetKeyDown("a"))
{
anim.Play("anim_runa_run_left");
}
}
private void FixedUpdate()
{
if (Input.GetKeyUp("d"))
{
anim.Play("anim_runa_idle");
}
if (Input.GetKeyUp("a"))
{
anim.Play("anim_runa_idle_left");
}
}
}
Any and all help would be much appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130800im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flipping Animation/Jumping animation 0 Answers
Walking animation javascript? 2 Answers
WTF does this error mean 1 Answer
Help with script for Main Menu? 1 Answer
How can I detect what animation condition an animator is in through scripting? 0 Answers