【问题标题】:Smoothing walk animations in unity (2D Unity in C#)统一平滑行走动画(C# 中的 2D Unity)
【发布时间】:2016-08-21 02:35:46
【问题描述】:

所以我们要做的是让我们的玩家角色在行走时平稳移动。步行由用户通过箭头键输入。她有一个动画课程和一个对撞机课程,以保持她的步行周期并让她留在游戏板上。问题是它一次只会移动一个抽搐动作,而不是在箭头键按下帮助时继续移动。有什么建议吗?

using UnityEngine;
using System.Collections;

public class GridMove : MonoBehaviour
{

    private float moveSpeed = 128f;
    private float gridSize = 64f;
    private enum Orientation { Horizontal, Vertical };

    private Orientation gridOrientation = Orientation.Horizontal;
    private bool allowDiagonals = false;
    private bool correctDiagonalSpeed = true;
    private Vector2 input;
    public bool isMoving = false;
    private Vector3 startPosition, endPosition;
    private float t;
    private float factor;

    public bool wallLeft = false;
    public bool wallRight = false;
    public bool wallUp = false;
    public bool wallDown = false;

    void Start()
    {
        startPosition = transform.position;

    }
    // Update is called once per frame
    void Update()
    {
        CheckInput();


        if (isMoving)
        {
            transform.position = endPosition;
            isMoving = false;
        }

        if (!isMoving)
        {
            input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            if (!allowDiagonals)
            {
                if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
                {
                    input.y = 0;
                }
                else
                {
                    input.x = 0;
                }
            }

            if (input != Vector2.zero)
            {
                StartCoroutine(move(transform));
            }
        }

    }

    public IEnumerator move(Transform transform)
    {
        isMoving = true;
        startPosition = transform.position;
        t = 0;

        if (allowDiagonals && correctDiagonalSpeed && input.x != 0 && input.y != 0)
        {
            factor = 0.7071f;
        }
        else
        {
            factor = 1f;
        }

        while (t < 1f)
        {
            t += Time.deltaTime * (moveSpeed / gridSize) * factor;
            transform.position = Vector3.Lerp(startPosition, endPosition, t);
            yield return null;
        }

        isMoving = false;
        yield return 0;
    }
    private void CheckInput()
    {
        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) && wallRight == false)
        {
            endPosition += Vector3.right * gridSize;
            isMoving = true;
        }
        else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) && wallLeft == false)
        {
            endPosition -= Vector3.right * gridSize;
            isMoving = true;
        }
        else if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) && wallUp == false)
        {
            endPosition += Vector3.up * gridSize;
            isMoving = true;
        }
        else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow) && wallDown == false)
        {
            endPosition -= Vector3.up * gridSize;
            isMoving = true;
        }
    }

}

对撞机脚本

using UnityEngine;
using System.Collections;

public class Collider : MonoBehaviour 
{
    void OnTriggerEnter2D(Collider2D col)
    {
        Debug.Log("enter");

        if (col.CompareTag("left wall"))
        {
            Debug.Log("i see a little sillouetto of a man");
            GetComponent<GridMove>().wallLeft = true;
            Debug.Log("left");
        }
        else if (col.CompareTag("right wall"))
        {
            GetComponent<GridMove>().wallRight = true;
        }
        else if (col.CompareTag("up wall"))
        {
            GetComponent<GridMove>().wallUp = true;
        }
        else if (col.CompareTag("down wall"))
        {
            GetComponent<GridMove>().wallDown = true;
        }
    }

    void OnTriggerExit2D(Collider2D col)
    {
        if (col.CompareTag("left wall"))
        {
            GetComponent<GridMove>().wallLeft = false;
        }
        else if (col.CompareTag("right wall"))
        {
            GetComponent<GridMove>().wallRight = false;
        }
        else if (col.CompareTag("up wall"))
        {
            GetComponent<GridMove>().wallUp = false;
        }
        else if (col.CompareTag("down wall"))
        {
            GetComponent<GridMove>().wallDown = false;
        }
    }

}

【问题讨论】:

  • 是动画不流畅还是只是运动/速度流畅?
  • 我真的不明白为什么您在更新中使用该协程,而不是只要按住按钮,每帧可以移动多远。我很确定这个问题也埋在那里,但只要我不明白为什么它需要在那里,我就无法提供任何进一步的建议。

标签: c# animation unity5 unity3d lerp


【解决方案1】:

您的代码还有一些问题需要清理。但是要解决您的代码问题,您只需将更新循环更改为以下内容。

void Update()
{
    if(!isMoving) 
    {
        CheckInput();

        if(isMoving) 
        {
            input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            if(!allowDiagonals) 
            {
                if(Mathf.Abs(input.x) > Mathf.Abs(input.y)) 
                {
                    input.y = 0;
                } else 
                {
                    input.x = 0;
                }
            }

            StartCoroutine(move(transform));
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多