我有以下blendtree尝试根据移动方向更改动画。
我有一个技师用以下代码单击鼠标来移动对象:
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class ClickMovement : MonoBehaviour
{
public float speed = 2;
public Animator animator;
private Vector3 target;
public Rigidbody2D rb;
void Start()
{
target = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
animator.SetFloat("Horizontal", transform.position.x);
animator.SetFloat("Vertical", transform.position.y);
animator.SetFloat("Magnitude", transform.position.magnitude);
}
}
尽管我运行场景时,精灵会随意地遍历动画。
我想这就是我在代码中派生水平和垂直浮动的方式。有人可以帮我吗?非常感谢
您可能想使用朝向目标的方向而不是位置。
Vector3 dir = target - transform.position;
dir.Normalize();
animator.SetFloat("Horizontal", dir.x);
animator.SetFloat("Vertical", dir.y);
animator.SetFloat("Magnitude", dir.magnitude);