我需要有人来帮助我处理Unity3d中的transform.position。我试图按M键时停止tranform.position。我尝试使用下面提到的代码,但它仍以相同的速度移动。
if (Input.GetKeyDown(KeyCode.M))
{
transform.position += PlayerController.player.transform.forward * -0.00001f;
}
else
{
transform.position += PlayerController.player.transform.forward * -0.1f;
}
在正常情况下,transform.position可以正常工作,但是当我按M键时,它的速度应该慢到现在看起来已经停止了。我认为其他条件运行良好。
Currently you reach your
if
block exctly in one single frame namely the moment the key goes down the first time.Input.GetKeyDown
For continues executions you would want to use
Input.GetKey
whichBtw if you really want to
stop
the movement as you say you would probably rather simply useand do nothing while
M
stays pressedNote that your code is frame-rate dependent and you always should use
* Time.deltaTime
for converting your values fromUnits / frame
intoUnits / second
You should use
GetKey
insteadYou are using
GetKeyDown
that only returns true during the frame you pressed the key resulting in slowdown only one frame ¬https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html