C# I';I’’我试着把球员向前推一段固定的距离,但只起作用一次。如何允许玩家多次移动?
请检查代码。一旦玩家移动固定距离一次,即使在给定空间输入的情况下,他也不会再移动。 我怎样才能在玩家被移动一次后继续移动玩家C# I';I’’我试着把球员向前推一段固定的距离,但只起作用一次。如何允许玩家多次移动?,c#,unity3d,game-development,C#,Unity3d,Game Development,请检查代码。一旦玩家移动固定距离一次,即使在给定空间输入的情况下,他也不会再移动。 我怎样才能在玩家被移动一次后继续移动玩家 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovementPC : MonoBehaviour { [Header ("Move Settings")] private Vector3 Starti
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementPC : MonoBehaviour
{
[Header ("Move Settings")]
private Vector3 StartingPosition, EndingPosition;
public float moveDistance = 30f;
public float LerpTime = 1f;
private float CurrentLerpTime = 0;
public bool movePressed = false;
private void Start()
{
StartingPosition = transform.position;
EndingPosition = transform.position + Vector3.forward * moveDistance;
}
void Update()
{
if (Input.GetKeyDown("space"))
{
movePressed = true;
Debug.Log("dash pressed");
}
if (movePressed == true)
{
CurrentLerpTime += Time.unscaledDeltaTime;
if (CurrentLerpTime >= LerpTime)
{
CurrentLerpTime = LerpTime;
}
float LerpPercentage = CurrentLerpTime / LerpTime;
transform.position = Vector3.Lerp(StartingPosition, EndingPosition, LerpPercentage);
}
}
任何帮助都将不胜感激。谢谢你抽出时间 问题在于您没有重置控制移动开始/结束位置的变量。将对象添加到关卡或按Play键时,将调用
Start()
方法,这是当前唯一重置这些值的方法。因此,如果要重置这些值,则需要确定移动何时完成,例如:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementPC : MonoBehaviour
{
[Header ("Move Settings")]
private Vector3 StartingPosition, EndingPosition;
public float moveDistance = 30f;
public float LerpTime = 1f;
private float CurrentLerpTime = 0;
private void Start()
{
ResetPositions();
}
void Update()
{
if (Input.GetKeyDown("space"))
{
CurrentLerpTime += Time.unscaledDeltaTime;
if (CurrentLerpTime >= LerpTime)
{
ResetPositions();
return;
}
float LerpPercentage = CurrentLerpTime / LerpTime;
transform.position = Vector3.Lerp(StartingPosition, EndingPosition, LerpPercentage);
}
}
void ResetPositions()
{
StartingPosition = transform.position;
EndingPosition = transform.position + Vector3.forward * moveDistance;
CurrentLerpTime = 0;
}
好的,一旦你到了结束位置,没有任何东西会改变这个位置,所以它不会再移动。当你检测到鼠标向下移动,当鼠标到达结束位置时,你可能想设置开始和结束位置。按下False键。谢谢你的回复@BugFinder,我试过它没有按预期工作,还有其他想法吗?没有tbh没有足够的细节来知道发生了什么或没有发生什么您没有将movePressed重置为false。@Kamiky我尝试添加它,但它没有按预期工作,并开始将我循环回startpos