Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 具有点击运动的动画[Unity2D]_Visual Studio_Unity3d_Animation_Transform_Animator - Fatal编程技术网

Visual studio 具有点击运动的动画[Unity2D]

Visual studio 具有点击运动的动画[Unity2D],visual-studio,unity3d,animation,transform,animator,Visual Studio,Unity3d,Animation,Transform,Animator,我使用下面的blendtree尝试根据运动方向更改动画 我有一个机械师用鼠标点击移动对象,代码如下: using System.Collections; using System.Collections.Generic; using System.Security.Cryptography; using UnityEngine; public class ClickMovement : MonoBehaviour { public float speed = 2; publi

我使用下面的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);

作为奖励,是否有办法将空闲状态设置为精灵最后一个行驶方向?@Idrawthings您可以基于最后一个非零量级方向保存最后一个朝向
if(dir.magnity>0f)lastDir=dir
。然后将其应用于空闲混合树。lastDir是函数吗?我的控制台显示“名称'lastDir'在当前上下文中不存在”。它必须是Vector3类型的新变量。