0
我有一個腳本來動我的角色(玩家) 腳本應該是罰款,它沒有任何錯誤,但是當我按下播放鍵我嘗試使用箭頭,它不工作,我不知道爲什麼。團結球員的跑動不工作
這是代碼。我感謝所有幫助你可以給我,謝謝
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Direction currentDir;
Vector2 input;
bool isMoving = false;
Vector3 startPos;
Vector3 endPos;
float t;
public float walkSpeed = 3f;
// Update is called once per frame
void Update()
{
if (isMoving)
{
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (Mathf.Abs(input.x) > input.y)
input.y = 0;
else
input.x = 0;
if (input != Vector2.zero)
{
StartCoroutine(Move(transform));
}
}
}
public IEnumerator Move(Transform entity)
{
isMoving = true;
startPos = entity.position;
t = 0;
endPos = new Vector3(startPos.x + System.Math.Sign(input.x), startPos.y +
System.Math.Sign(input.y), startPos.z);
while (t < 1f)
{
t += Time.deltaTime * walkSpeed;
entity.position = Vector3.Lerp(startPos, endPos, t);
yield return null;
}
isMoving = false;
yield return 0;
}
enum Direction
{
North,
East,
South,
West
}
}
這是錯的。在更新功能中重複啓動「移動」協同程序.....您正在嘗試像在FPS遊戲中一樣移動? – Programmer
我試圖讓它像2D遊戲一樣移動。爲了爭吵讓我們說像口袋妖怪:) –
好吧,我明白了。如果是這樣的話,這可能是好的。你如何確定停止移動/目的地位置?我想你應該解釋玩家何時開始移動?也許當鼠標點擊? – Programmer