我在网上找到了这段代码
// Do not apply input if we are showing a level selection display
if(HaltUpdateMovement == true)
return;
bool moveForward = false;
bool moveLeft = false;
bool moveRight = false;
bool moveBack = false;
MoveScale = 1.0f;
// * * * * * * * * * * *
// Keyboard input
// Move
// WASD
if (Input.GetKeyDown(KeyCode.W)) moveForward = true;
if (Input.GetKey(KeyCode.A)) moveLeft = true;
if (Input.GetKey(KeyCode.S)) moveBack = true;
if (Input.GetKey(KeyCode.D)) moveRight = true;
// Arrow keys
if (Input.GetKeyDown(KeyCode.UpArrow)) moveForward = true;
if (Input.GetKey(KeyCode.LeftArrow)) moveLeft = true;
if (Input.GetKey(KeyCode.DownArrow)) moveBack = true;
if (Input.GetKey(KeyCode.RightArrow)) moveRight = true;
if ( (moveForward && moveLeft) || (moveForward && moveRight) ||
(moveBack && moveLeft) || (moveBack && moveRight) )
MoveScale = 0.70710678f;
// No positional movement if we are in the air
if (!Controller.isGrounded)
MoveScale = 0.0f;
MoveScale *= DeltaTime;
// Compute this for key movement
float moveInfluence = Acceleration * 0.1f * MoveScale * MoveScaleMultiplier;
// Run!
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
moveInfluence *= 2.0f;
if(DirXform != null)
{
if (moveForward)
MoveThrottle += DirXform.TransformDirection(Vector3.forward * moveInfluence);
if (moveBack)
MoveThrottle += DirXform.TransformDirection(Vector3.back * moveInfluence) * BackAndSideDampen;
if (moveLeft)
MoveThrottle += DirXform.TransformDirection(Vector3.left * moveInfluence) * BackAndSideDampen;
if (moveRight)
MoveThrottle += DirXform.TransformDirection(Vector3.right * moveInfluence) * BackAndSideDampen;
}
我的问题是,一旦我按下w,我希望我的人不停地前进,但是他只是向前移动一个勾号然后再次停止,任何想法/帮助?
答案 0 :(得分:3)
使用GetKey
代替GetKeyDown
。 GetKeyDown
仅在用户按下键时在帧中返回true ,而只要键被保持,GetKey
将继续返回true。
请参阅:http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKey.html
并且:http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKeyDown.html
答案 1 :(得分:0)
moveForward
在哪里设置为false
?它似乎不应该发生。或者它的范围错误。它是局部变量(即在方法中声明)还是类中的字段(即直接在类中声明)?后者可能就是你要找的东西。
bool moveForward = false;
void Update() // or whatever method you had your code in
{
bool moveLeft = false;
bool moveRight = false;
bool moveBack = false;
// do stuff
}