Created
May 1, 2019 10:45
-
-
Save naojitaniguchi/b674af717f4591023d4895300bbbf19d to your computer and use it in GitHub Desktop.
Change Animation trigger script for 2D character in Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class AnimChange : MonoBehaviour | |
{ | |
Animator animator; | |
bool moving = false; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
animator = GetComponent<Animator>(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
moving = false; | |
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || | |
Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow)) | |
{ | |
moving = true; | |
} | |
if (!moving) | |
{ | |
animator.SetTrigger("Idle"); | |
} | |
if (Input.GetKeyDown(KeyCode.UpArrow)) | |
{ | |
animator.ResetTrigger("Idle"); | |
animator.SetTrigger("Up"); | |
} | |
if (Input.GetKeyDown(KeyCode.DownArrow)) | |
{ | |
animator.ResetTrigger("Idle"); | |
animator.SetTrigger("Front"); | |
} | |
if (Input.GetKeyDown(KeyCode.LeftArrow)) | |
{ | |
animator.ResetTrigger("Idle"); | |
animator.SetTrigger("Left"); | |
} | |
if (Input.GetKeyDown(KeyCode.RightArrow)) | |
{ | |
animator.ResetTrigger("Idle"); | |
animator.SetTrigger("Right"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment