所以我们尝试做的就是让我们的球员角色在行走时保持平稳运动。步行由用户通过箭头键输入决定。她有一个动画课程和一个与她相关的对撞机课程,以保持她的步行循环并将她留在游戏板上。问题是它一次只能移动一个抽搐动作,而不是在箭头键帮助时继续移动。有什么建议吗?
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;
}
}
}
COLLIDER SCRIPT
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;
}
}
}
答案 0 :(得分:0)
您的代码还有一些可以清理的问题。但要解决您的代码问题,您只需将更新循环更改为以下内容。
restart the device