1
私たちがやっていることは、私たちのプレイヤーキャラクターが歩いているときに滑らかな動きをすることです。歩行は、矢印キーを介したユーザ入力によって指示される。彼女は歩くサイクルを続けるためにアニメーションクラスとコライダークラスを付けてゲームボードに置いています。問題は、矢印キーが助けになったときに移動を継続するのではなく、一度に1つの急な動きしか動かさないことです。助言がありますか?スムージングウォークアニメーションをユニティで表現する(C#の2D Unity)
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