1

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

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

}
4

1 回答 1

0

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

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));
        }
    }
}
于 2016-05-12T07:02:08.560 回答