How to stop animations from cancelling out each other - C#
Hi, I'm new to Unity and game engines in general. I'm trying to make a 3rd person 1v1 sort of game, but I'm stuck with animations. I have set up basic movement animations but when I would press a key such as 'W' to move the character forward, the walk animation would play forever. Furthermore, if I press 'Space' to Jump and then right after press another animation key, the Jump animation would instantly cancel out and play the other animation.
What I'm wondering is how do I stop the 'Jump' animation from canceling when I press another key and how do I make movement animation play only when holding down the key and stopping them when I let go. (FYI, the anim.Stop command doesn't work for some reason). I would greatly appreciate it if someone could help me out!
Thanks a lot!
My animation code is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KnightAnim : MonoBehaviour {
private Animator anim;
void Start () {
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetButtonDown ("Jump"))
anim.Play ("Jump");
if (Input.GetKeyDown(KeyCode.UpArrow))
anim.Play ("Walk");
if (Input.GetKeyDown (KeyCode.W))
anim.Play ("Walk");
if (Input.GetKeyDown(KeyCode.Q))
anim.Play ("Run");
if (Input.GetKeyDown(KeyCode.LeftArrow))
anim.Play ("StrafeLeft");
if (Input.GetKeyDown(KeyCode.A))
anim.Play ("StrafeLeft");
if (Input.GetKeyDown(KeyCode.RightArrow))
anim.Play ("StrafeRight");
if (Input.GetKeyDown(KeyCode.D))
anim.Play ("StrafeRight");
if (Input.GetKeyDown(KeyCode.DownArrow))
anim.Play ("WalkBack");
if (Input.GetKeyDown(KeyCode.S))
anim.Play ("WalkBack");
if (Input.GetKeyDown (KeyCode.E))
anim.Play ("RunJump");
}
}
Answer by PersianKiller · Sep 13, 2017 at 07:11 AM
hey dude,you should create a condition to stop playing animation when you're not pushing keys.
watch this it might help.this is not an English site. first click on 1,then click on 2 to download the tutorial.
http://s9.picofile.com/file/8306314718/bandicam_2017_09_13_00_53_30_427.mp4.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour {
public Animator aa;
// Use this for initialization
void Start () {
aa = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw ("Horizontal") == 0) {
//it means that youre not pushing move keys [ a left arrow , d right arrow]
aa.Play("idle");
}
if (Input.GetAxisRaw ("Horizontal") != 0) {
//it means that youre pushing move keys [ a left arrow , d right arrow]
aa.Play("run");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612134634im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to start an animation in script? 2 Answers
Player animation doesn't want to stop 0 Answers
How to modify a value after the animation is done with it. 0 Answers
Interpolated movement in a circle 1 Answer
ANIMATION STRANGELY DOES NOT PLAY 0 Answers