1
所以我們要做的就是讓我們的玩家角色在走路時能夠平滑移動。步行由用戶通過箭頭鍵輸入口述。她有一個動畫課程和一個對撞機班,以保持她的步行週期,並讓她留在遊戲板上。問題在於,它只會一次移動一個猛擊動作,而不是在箭頭鍵向下幫助時繼續移動。有什麼建議麼?平滑步行動畫統一(2D Unity在C#中)
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;
}
}
}
撞機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;
}
}
}
它是動畫不平滑還是隻是運動/速度平滑? – 111WARLOCK111
我真的不明白你爲什麼在更新中使用該協程,而不是隻要按住按鈕就可以移動每幀的距離。我很確定這個問題也存在,但只要我不明白爲什麼它需要在那裏,我不能提供任何進一步的建議。 – yes