单击以移动不起作用的动画

本文关键字:动画 不起作用 移动 单击 | 更新日期: 2023-09-27 18:25:48

我的游戏的基础是一个简单的2D自上而下点击移动游戏I。我创建了一个行走混合树和一个空闲混合树。两种混合树都有6个方向的移动。我看了这个关于自上而下8个方向运动的教程。(https://www.youtube.com/watch?v=7URRg8J6mz8)。基本上,我会更改脚本,使其与我的点击移动脚本相匹配,但它并不是100%完美的。我的空闲状态工作不正常,或者我的Y轴也不正常,更具体地说,当我长按Y轴时(假设我要向上(+)),它并没有在动画师中真正播放正确的动画。(现在假设我往下看)它会播放正确的动画,但(当完成移动时)它会继续播放动画。我的参数也不正常工作,SpeedY和LastMoveY不稳定(如果我有任何意义的话)。有人能帮我修一下吗?如果有人不明白我的意思,这是我在谷歌硬盘上传的游戏预览!http://html.editey.com/file/0B6cfYGCOex7BVC1Ja3Q3N3d3NWc#

using UnityEngine;
using System.Collections;
public class move : MonoBehaviour {
private Animator anim;
public float speed = 15f;
public move playerMovementRef;
private Vector3 target;
private Vector3 playerObject;

void Start () {
    target = transform.position;
    anim = GetComponent<Animator> ();
}
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);
    float inputX = Input.GetAxis ("Mouse X");
    float inputY = Input.GetAxis ("Mouse Y");
    if (Input.touchCount > 0)
    {
        inputX = Input.touches[0].deltaPosition.x;
        inputY = Input.touches[0].deltaPosition.y;
    }
    anim.SetFloat ("SpeedX", inputX);
    anim.SetFloat ("SpeedY", inputY);
}
void FixedUpdate () {
    float LastInputX = Input.GetAxis ("Mouse X");
    float LastInputY = Input.GetAxis ("Mouse Y");
    if (Input.touchCount > 0)
    {
        LastInputX = Input.touches[0].deltaPosition.x;
        LastInputY = Input.touches[0].deltaPosition.y;
    }
    if (LastInputX != 0 || LastInputY != 0) {
        anim.SetBool ("walking", true);
        if (LastInputX > 0) {
            anim.SetFloat ("LastMoveX", 1f);
        } else if (LastInputX < 0) {
            anim.SetFloat ("LastMoveX", -1f);
        } else {
            anim.SetBool ("walking", false);
        }
        if (LastInputY > 0) {
            anim.SetFloat ("LastMoveY", 1f);
        } else if (LastInputY < 0) {
            anim.SetFloat ("LastMoveY", -1f);
        } else {
            anim.SetFloat ("LastMoveY", 0f);
        }
    } else {
        anim.SetBool ("walking", false);
    }
}

单击以移动不起作用的动画

您不应该在FixedUpdate中使用Input值。您应该将它存储在Update中的一个变量中,然后在FixedUpdate中检查它。

您还应该根据您的相机为鼠标位置添加一些深度。

错误:

void FixedUpdate () {
       if (Input.touchCount > 0)
       {
           ...
       }
}

正确:

    void Update () {
           ...
           if (Input.touchCount > 0)
           {
                touched = true;
           }
    }
    void FixedUpdate () {
           if (touched)
           {
                ...
           }
    }

示例:

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Vector3 mousePosition = Input.mousePosition;
        mousePosition.z = 10; // distance from the camera
        target = Camera.main.ScreenToWorldPoint(mousePosition);
        target.z = transform.position.z;
    }
    if (Input.touchCount > 0)
    {
        target.x = Input.touches[0].deltaPosition.x;
        target.y = Input.touches[0].deltaPosition.y;
    }
    transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
void FixedUpdate()
{
    float LastInputX = transform.position.x - target.x;
    float LastInputY = transform.position.y - target.y;
    if (LastInputX != 0 || LastInputY != 0)
    {
        anim.SetBool("walking", true);
        if (LastInputX > 0)
        {
            anim.SetFloat("LastMoveX", 1f);
        }
        else if (LastInputX < 0)
        {
            anim.SetFloat("LastMoveX", -1f);
        }
        else {
            anim.SetBool("walking", false);
        }
        if (LastInputY > 0)
        {
            anim.SetFloat("LastMoveY", 1f);
        }
        else if (LastInputY < 0)
        {
            anim.SetFloat("LastMoveY", -1f);
        }
        else {
            anim.SetFloat("LastMoveY", 0f);
        }
    }
    else {
        anim.SetBool("walking", false);
    }
}