我在c#中为玩家动作编写了一个脚本。每当玩家按下A或D时,它将他/她向左或向右移动12个单位,当玩家按下W或S时,它将他/她向上或向下移动12个单位。我的脚本工作正常,但是如果一个人开始一次性垃圾邮件所有的密钥,它就会出现故障并且播放器对象不再与该级别保持一致。我想让脚本在按键上执行移动之前检查当前是否发生了移动。这是我的剧本:
<form method="post" enctype= multipart/form-data action="<?php echo $_SERVER['PHP_SELF']; ?>" id="newstatus" runat="server">
答案 0 :(得分:2)
您可以使用简单的isMoving
变量。如果变量为true
,请勿移动。如果是false
则移动。协程完成后,将isMoving
设置回false
。另外,我无法说明MoveObject
函数为static
的原因。我从中删除了static
关键字。
bool isMoving = false;
void Update()
{
transform.Translate(Vector3.forward * ForwardSpeed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.A) && side > maxSideLeft)
{
MoveObjectTo(this.transform, new Vector3(this.transform.position.x - 12, this.transform.position.y, this.transform.position.z + 10), movementSpeed);
side -= 1;
}
else if (Input.GetKeyDown(KeyCode.D) && side < maxSideRight)
{
MoveObjectTo(this.transform, new Vector3(this.transform.position.x + 12, this.transform.position.y, this.transform.position.z + 10), movementSpeed);
side += 1;
}
if (Input.GetKeyDown(KeyCode.W) && level < maxLevelHeight)
{
MoveObjectTo(this.transform, new Vector3(this.transform.position.x, this.transform.position.y + 12, this.transform.position.z + 10), movementSpeed);
level += 1;
}
else if (Input.GetKeyDown(KeyCode.S) && level > minLevelHeight)
{
MoveObjectTo(this.transform, new Vector3(this.transform.position.x, this.transform.position.y - 12, this.transform.position.z + 10), movementSpeed);
level -= 1;
}
if (Input.GetKeyDown(KeyCode.R) || Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene("Scene1");
Time.timeScale = 1;
}
}
private void MoveObjectTo(Transform objectToMove, Vector3 targetPosition, float moveSpeed)
{
//Don't do anything Object is already moving
if (isMoving)
{
return;
}
//If not moving set isMoving to true
isMoving = true;
StartCoroutine(MoveObject(objectToMove, targetPosition, moveSpeed));
}
public IEnumerator MoveObject(Transform objectToMove, Vector3 targetPosition, float moveSpeed)
{
float currentProgress = 0;
Vector3 cashedObjectPosition = objectToMove.transform.position;
while (currentProgress <= 1)
{
currentProgress += moveSpeed * Time.deltaTime;
objectToMove.position = Vector3.Lerp(cashedObjectPosition, targetPosition, currentProgress);
yield return null;
}
//Done moving. Set isMoving to false
isMoving = false;
}
答案 1 :(得分:0)
听起来你想要检查用户是否已经完成了他们的lerp,然后才能进入某个新位置。在这种情况下,您将希望阻止您的播放器再次移动,直到您的currentProgress
变量等于1.因此,使用简单的布尔回调并阻止调用Corouting直到该回调为真。您的MoveObject
功能:
public static IEnumerator MoveObject(Transform objectToMove, Vector3 targetPosition, float moveSpeed, System.Action<bool> callBack)
{
float currentProgress = 0;
Vector3 cashedObjectPosition = objectToMove.transform.position;
while (currentProgress <= 1)
{
currentProgress += moveSpeed * Time.deltaTime;
objectToMove.position = Vector3.Lerp(cashedObjectPosition, targetPosition, currentProgress);
yield return null;
if (currentProgress >= 1)
callBack(true);
}
}
然后您可以更改MoveObjectTo
功能以阅读此回调:
private void MoveObjectTo(Transform objectToMove, Vector3 targetPosition, float moveSpeed)
{
if (canCallCoroutine)
{
canCallCoroutine = false;
StartCoroutine(MoveObject(objectToMove, targetPosition, moveSpeed, finished =>
{
if (finished != null)
{
if (finished)
canCallCoroutine = true;
}
}));
}
}