具有点击动作的动画[Unity2D]

问题描述 投票:1回答:1

我有以下blendtree尝试根据移动方向更改动画。

enter image description here

我有一个技师用以下代码单击鼠标来移动对象:

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);

    }
}

尽管我运行场景时,精灵会随意地遍历动画。

我想这就是我在代码中派生水平和垂直浮动的方式。有人可以帮我吗?非常感谢

visual-studio unity3d animation transform animator
1个回答
0
投票

您可能想使用朝向目标的方向而不是位置。

Vector3 dir = target - transform.position;
dir.Normalize();

animator.SetFloat("Horizontal", dir.x);
animator.SetFloat("Vertical", dir.y);
animator.SetFloat("Magnitude", dir.magnitude);
© www.soinside.com 2019 - 2024. All rights reserved.