Horizental and Vertical Animation

Im making a top down 2d shooter, and I got 4 animations, an up animation, a down animation a left animation and a right animation, now I have this movement script:

[script]
using UnityEngine;
using System.Collections;

public class EasyMove : MonoBehaviour {

public float Speed = 0f;
private float movex = 0f;
private float movey = 0f;

// Use this for initialization
void Start () {
}

// Update is called once per frame
void FixedUpdate () {
movex = Input.GetAxis (“Horizontal”);
movey = Input.GetAxis (“Vertical”);
GetComponent().velocity = new Vector2 (movex * Speed, movey * Speed);
}
}

[script]

But I want the animations implented but how can this be done with Horizontal and Vertical properties :confused:

Call your animations with myAnimation AnimationClip; and then you could probably just check for WASD keypresses, and play the appropriate animation. Make sure that the animation stops on another keypress and that it loops if the key is held down.

Animator anim = GetComponent<Animator>();
if (abs(movex) > abs(movey)) {
  // Moving horizontally
  if (movex<0) anim.setInt("direction", 0);
  else anim.setInt("direction", 1);
} else {
  // Moving vertically
  if (movey<0) anim.setInt("direction", 2);
  else anim.setInt("direction", 3);
}

Then check the transitions in Animator for
Anystate → 0 → Left anim
Anystate → 1 → Right anim
Anystate → 2 → Up anim
Anystate → 3 → Down anim

XD I normally use legacy but its time to learn to work with the animator :stuck_out_tongue:
thank you allot for the script :slight_smile: Have an amazing day Zalflis

Oh, and the abs() was something like Mathf.Abs() used for just seeing if horizontal or vertical movement is stronger :slight_smile: Just don’t remember everything from memory.

1 Like