Điều khiển Animator (Trigger, Bool, Blend Tree)
C# Code
using UnityEngine;
public class PlayerAnimatorController : MonoBehaviour
{
private Animator animator;
private float moveSpeed;
void Awake()
{
animator = GetComponent<Animator>();
}
void Update()
{
// --- Bool: Chuyển đổi giữa chạy và đứng yên ---
bool isMoving = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D);
animator.SetBool("IsRunning", isMoving);
// --- Trigger: Kích hoạt animation tấn công ---
if (Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("Attack");
}
// --- Float for Blend Tree: Điều khiển tốc độ di chuyển ---
// Giả sử bạn có một Blend Tree 1D tên là "MoveSpeed"
// điều khiển animation đi bộ/chạy dựa trên tốc độ.
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector2 inputVector = new Vector2(horizontalInput, verticalInput);
moveSpeed = inputVector.magnitude;
animator.SetFloat("MoveSpeed", moveSpeed);
}
}Ví dụ cách sử dụng các tham số Trigger, Bool và Float (cho Blend Tree) để điều khiển các trạng thái animation của nhân vật.