Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 在一个方向保持恒定速度_Unity3d - Fatal编程技术网

Unity3d 在一个方向保持恒定速度

Unity3d 在一个方向保持恒定速度,unity3d,Unity3d,我试图制作一个玩家只控制球左右移动的游戏()。球应始终以恒定速度向前移动。到目前为止,我已经尝试了以下方法,但我只能在球在空中时左右控制球(跳跃) 任何帮助或链接将非常感谢 float forwardVelocity = 20.0f void Update () { if (gameConfig.currentGameState == GameConfig.CurrentGameState.LevelInPlay) { handleMovemen

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

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

 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);
     }
 }
float forwardVelocity=20.0f
无效更新()
{
如果(gameConfig.currentGameState==gameConfig.currentGameState.LevelInPlay)
{
手部运动();
手跳();
死亡检测();
}
}
void handleJumping()
{
if((Input.GetMouseButtonDown(0)| | Input.GetButtonDown(“跳转”)&&isGrounded)
{
rigidBody.velocity=新矢量3(rigidBody.velocity.x,跳跃高度,rigidBody.velocity.z);
跳音。播放();
}
}
无效手动移动()
{
var moveHorizontal=Input.GetAxis(“水平”);
矢量3移动=新矢量3(水平移动,0.0f,0.0f);
rigidBody.AddForce(移动*旋转速度);
if(刚体速度z<前进速度)
{
rigidBody.velocity=新矢量3(rigidBody.velocity.x,rigidBody.velocity.y,forwardVelocity);
}
}

我最终用FixeUpdate方法完成了这项工作,该方法运行良好

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

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

我认为这是因为你增加了力量,然后在速度更新之前覆盖它。好的接球。我会试着让他们四处走动,看看这是否有帮助。谢谢