gpt4 book ai didi

c# - 阻止玩家乱用会导致故障的移动键

转载 作者:行者123 更新时间:2023-11-30 23:18:53 25 4
gpt4 key购买 nike

我用 C# 编写了播放器移动脚本。每当玩家按下 A 或 D 时,它会将他/她向左或向右移动 12 个单位,当玩家按下 W 或 S 时,它会将他/她向上或向下移动 12 个单位。我的脚本运行良好,但如果有人开始一次向所有按键发送垃圾邮件,它就会出现故障,玩家对象不再符合关卡。我想让脚本在按键上执行移动之前检查当前是否正在发生移动。这是我的脚本:

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)
{
StopCoroutine(MoveObject(objectToMove, targetPosition, moveSpeed));
StartCoroutine(MoveObject(objectToMove, targetPosition, moveSpeed));
}

public static 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;
}
}

最佳答案

您可以为此使用一个简单的 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;
}

关于c# - 阻止玩家乱用会导致故障的移动键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com