Question by
TigerTob · Mar 05, 2020 at 04:21 PM ·
animationmovementblend treeidle animations
8 Directional movement animations for mobile
Hello, I am making a mobile game with a top down 8 directional movement system. The movement and the movement animations work great. My problem is with the Idle animations, as they will only show the diagonal ones. I am using blend trees for the animations.
Here is my Script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class Character : MonoBehaviour {
float dirX, dirY, rotateAngle;
public int maxHealth;
public int currentHealth;
public int gold;
public GameObject hitEffect;
public HealthBar healthBar;
public Animator anim;
[SerializeField]
float moveSpeed = 2f;
[SerializeField]
float bulletSpeed = 5f;
[SerializeField]
Rigidbody2D bullet;
void Start () {
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
rotateAngle = 0f;
anim = GetComponent<Animator>();
anim.speed = 1;
}
void Update () {
Move ();
//Fire ();
//Rotate ();
float lastInputX = CrossPlatformInputManager.GetAxis("Horizontal");
float lastInputY = CrossPlatformInputManager.GetAxis("Vertical");
if (lastInputX != 0 || lastInputY != 0)
{
anim.SetBool("Walking", true);
if (lastInputX > 0)
{
anim.SetFloat("LastMoveX", 1f);
} else if (lastInputX < 0)
{
anim.SetFloat("LastMoveX", -1f);
}else
{
anim.SetFloat("LastMoveX", 0f);
}
if (lastInputY > 0)
{
anim.SetFloat("LastMoveY", 1f);
}
else if (lastInputY < 0)
{
anim.SetFloat("LastMoveY", -1f);
}
else
{
anim.SetFloat("LastMoveY", 0f);
}
anim.Play("Walk");
}
else
{
anim.SetBool("Walking", false);
anim.Play("Idle");
}
float inputX = CrossPlatformInputManager.GetAxis("Horizontal");
float inputY = CrossPlatformInputManager.GetAxis("Vertical");
anim.SetFloat("SpeedX", inputX);
anim.SetFloat("SpeedY", inputY);
}
void Move()
{
dirX = Mathf.RoundToInt(CrossPlatformInputManager.GetAxis ("Horizontal"));
dirY = Mathf.RoundToInt(CrossPlatformInputManager.GetAxis ("Vertical"));
transform.position = new Vector2 (dirX * moveSpeed * Time.deltaTime + transform.position.x,
dirY * moveSpeed * Time.deltaTime + transform.position.y);
}
public void TakeDamage(int damage)
{
Instantiate(hitEffect, transform.position, Quaternion.identity);
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
if (currentHealth <= 0)
{
Destroy(gameObject);
Debug.Log("Dead");
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Coin"))
{
ScoreTextScript.coinAmount += 1;
Destroy(other.gameObject);
}
}
}
Comment