【问题标题】:Keep constant speed in one direction在一个方向上保持匀速
【发布时间】:2016-01-28 17:45:40
【问题描述】:

我正在尝试制作一个玩家只控制球的左右移动的游戏 (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);
     }
 }

【问题讨论】:

  • 我认为这是因为你在速度更新之前添加了力量然后覆盖它。
  • 好收获。我会尝试移动它们,看看是否有帮助。谢谢

标签: unity3d


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    相关资源
    最近更新 更多