Trying to make my 2d movements go with animations
Hello! So i'm trying to set up having a movement (translations, that's already working) working with specific animation loops. The problem is that when i'm trying to get the other buttons to work nothing happens. So far only d (right) works. The only reason i'm trying to get this to go back to 0 is so that the animation stops when the player releases the button.
Do you guys have any ideas or feedback ? I feel like its a pretty basic error but i really can't see it.
using UnityEngine; using System.Collections;
public class PlayerAnimation : MonoBehaviour {
private Animator animator;
public int AnimDirection;
// Use this for initialization
void Start()
{
animator = this.GetComponent<Animator>();
AnimDirection = 0;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("d"))
{
AnimDirection = 1;
animator.SetInteger("animDirection", 1);
}
else
{
AnimDirection = 0;
animator.SetInteger("animDirection", 0);
}
if (Input.GetKey("a"))
{
AnimDirection = 2;
}
else
{
AnimDirection = 0;
animator.SetInteger("animDirection", 0);
}
if (Input.GetKey("w"))
{
AnimDirection = 3;
}
else
{
AnimDirection = 0;
animator.SetInteger("animDirection", 0);
}
if (Input.GetKey("s"))
{
AnimDirection = 4;
}
if (Input.GetKey("d") && Input.GetKey("w"))
{
AnimDirection = 5;
}
if (Input.GetKey("w") && Input.GetKey("a"))
{
AnimDirection = 6;
}
if (Input.GetKey("a") && Input.GetKey("s"))
{
AnimDirection = 7;
}
if (Input.GetKey("s") && Input.GetKey("d"))
{
AnimDirection = 8;
}
if (Input.GetKeyUp("a") && Input.GetKeyUp("w") && Input.GetKeyUp("s") && Input.GetKeyUp("d"))
{
AnimDirection = 0;
}
}
}
Answer by Zoogyburger · Mar 17, 2016 at 03:21 AM
I strongly suggest you make your character animation system as in this video:
https://www.youtube.com/watch?v=TU6wflRqT5Q
Here's the script
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
float input_x = Input.GetAxisRaw("Horizontal");
float input_y = Input.GetAxisRaw("Vertical");
bool isWalking = (Mathf.Abs(input_x) + Mathf.Abs(input_y)) > 0;
anim.SetBool("isWalking", isWalking);
if(isWalking)
{
anim.SetFloat("x", input_x);
anim.SetFloat("y", input_y);
transform.position += new Vector3(input_x, input_y, 0).normalized * Time.deltaTime;
}
}
}