保持一个方向的恒定速度

时间:2016-01-28 17:45:40

标签: unity3d

我正在尝试制作一个游戏,玩家只能控制球的左右移动(like rolling sky)。球应始终以恒定速度向前移动。到目前为止,我已经尝试了以下但是我只能在空中(跳跃)时左右控制球。

非常感谢任何帮助或链接。

 float forwardVelocity = 20.0f

 void Update () 
 {
     if (gameConfig.currentGameState == GameConfig.CurrentGameState.LevelInPlay) 
     {
         handleMovement();
         handleJumping();
         deathDetection ();
     }
 }
 void handleJumping()
 {
     if ((Input.GetMouseButtonDown(0) || Input.GetButtonDown("Jump")) && isGrounded)
     {
         rigidBody.velocity = new Vector3(rigidBody.velocity.x, jumpHeight, rigidBody.velocity.z);
         jumpSound.Play();
     }
 }
 void handleMovement()
 {
     var moveHorizontal = Input.GetAxis ("Horizontal");
     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
     rigidBody.AddForce (movement * rotationSpeed);
     if (rigidBody.velocity.z < forwardVelocity) 
     {
         rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, forwardVelocity);
     }
 }

1 个答案:

答案 0 :(得分:0)

我最终在FixedUpdate方法中执行此操作,该方法运行良好。

    void handleMovement()
{
    moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = 1.0f

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rigidBody.AddForce (movement * rotationSpeed);
}