禁用玩家的对角线跟随



所以我为我的玩家准备了一个脚本,其中我禁用了对角线移动。然后,我为他的狗同伴创建了一个在世界上跟随他的剧本,它几乎是完美的,除了当我从左/右到上/下或从左/下改变方向时,狗会沿着对角线移动,看起来真的很奇怪,因为没有动画,它基本上在那里滑动。我试着禁用对角线,就像我为球员做的那样,但它不起作用。有没有办法做到这一点,或者只为狗添加对角线动画会更好?

public class Bowser : MonoBehaviour
{
public float speed;
private Transform target;
private Vector2 move;
private Animator anim;
private void Awake()
{
anim = GetComponent<Animator>();
}
// Start is called before the first frame update
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
move.x = Input.GetAxisRaw("Horizontal");
move.y = Input.GetAxisRaw("Vertical");
if (Vector2.Distance(transform.position, target.position) > 1.5)
{
// Code attempt to get rid of diagonal movement
if (move.x != 0) move.y = 0;
if (move != Vector2.zero)
{
anim.SetFloat("moveX", move.x);
anim.SetFloat("moveY", move.y);
anim.SetBool("moving", true);
}
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
else
anim.SetBool("moving", false);
}
}

与其只检查0,不如只使用更大的值

if (Mathf.Abs(move.x) > (Mathf.Abs(move.y)) move.y = 0;
else move.x = 0;

如果你只为狗提供对角线动画会更好

谢谢你的建议,最后我发现添加这个会变得更顺利


if (Mathf.Abs(move.x) > .01f)
targetPosition.y = transform.position.y;
if (Mathf.Abs(move.y) > .01f)
targetPosition.x = transform.position.x;

然后改变我所有的目标。定位到目标定位

最新更新