2013-07-29 37 views
1

現在我正在使用WASD鍵來控制對象。現在我想用遊戲杆來做同樣的事情。任何人都可以告訴我如何調整我的代碼來做到這一點?我想要做的就是用if(Input)空間替換if(Input.getKey)空間,該空間將使用操縱桿左右移動船隻。在C#中使用操縱桿來控制對象

void drive() 
{ 
    //decrease speed 
    if (Input.GetKeyDown(KeyCode.S)) 
    { 
     //in Drive 
     if (acceleration > 0 && 
      bucketMode == false) 
     { 
      acceleration-= 250; 
     } 
     //in Reverse 
     else if (acceleration < 0 && 
      bucketMode == true) 
     { 
      acceleration += 250; 
     } 
    } 

    //Increases Speed 
    if (Input.GetKeyDown(KeyCode.W)) 
    { 
     //in Drive 
     if (acceleration < maxSpeed && 
      bucketMode == false) 
     { 
      acceleration+= 250; 
     } 
     //in Reverse 
     else if (acceleration > minSpeed && 
      bucketMode == true) 
     { 
      acceleration -= 250; 
     } 
    } 

    //stops the boat and toggles Bucket mode 
    if (Input.GetKeyDown(KeyCode.Space)) 
    { 
     acceleration = 0; 
     if (bucketMode == false) 
     { 
      //goes into revers 
      bucketMode = true; 
      craft.bucket(1); //sends bucket as true 
     } 
     else 
     { 
      //goes into drive 
      bucketMode = false; 
      craft.bucket(0); //sends bucket as false 
     } 
    } 
    //moves forward if speed is + 
    if (acceleration < 0) 
    { 
     //moves backwards at half the speed than in drive, in a smooth motion, speed depending on user input(acceleration) 
     this.transform.Translate(Vector3.left * Time.smoothDeltaTime * ((-acceleration/500) +1)); 

     //updates craft speed being displayed 
     craft.ChangeSpeed((acceleration/500) * 1.94); 
    } 
    //moves backward if speed is - 
    else if (acceleration > 0) 
    { 
     //moves forward in a smoothb motion, speed depending on user input(acceleration) 
     this.transform.Translate(Vector3.right * Time.smoothDeltaTime * ((acceleration/250) + 1)); 

     //updates craft speed being displayed 
     craft.ChangeSpeed((acceleration/250) * 1.94); 
    } 
    //stands still if speed is 0 
    else 
    {   
    } 

    //turns right 
    if (Input.GetKey(KeyCode.D)) 
    { 
     //turns the boat to the left smoothly, as speed increases, so does turn angle 
     this.transform.Rotate(Vector3.forward * Time.smoothDeltaTime * 25 /* ((2 * acceleration/250) +1)*/); 

     //updates boat information(heading and speed) 
     craft.ChangeHeading(1 * Time.smoothDeltaTime * 25 /* ((2 * acceleration/250) +1)*/); 
     craft.ChangeSpeed((acceleration/250) * 1.94); 

     //if boat is not moving forwards, moves forward slowly, so boat doesn't turn in place 
     if (acceleration == 0) 
     { 
      this.transform.Translate(Vector3.right * Time.smoothDeltaTime/2); 
     } 
    } 
    //turns left 
    if (Input.GetKey(KeyCode.A)) 
    { 
     //turns the boat to the right smoothly, as speed increases, so does turn angle 
     this.transform.Rotate(Vector3.back * Time.smoothDeltaTime * 25 /* ((2 * acceleration/250) +1)*/); 

     //updates boat information(heading and speed) 
     craft.ChangeHeading(-1 * Time.smoothDeltaTime * 25 /* ((2 * acceleration/250) +1)*/); 
     craft.ChangeSpeed((acceleration/250) * 1.94); 

     //if boat is not moving forwards, moves forward slowly, so boat doesn't turn in place 
     if (acceleration == 0) 
     { 
      this.transform.Translate(Vector3.right * Time.smoothDeltaTime/2); 
     } 
    } 
} 
+0

代碼最重要的部分是if語句的A和D,我剛剛包含的所有內容,以便您可以看到船如何移動 – Flotolk

回答

3

你來到這裏的多個選項與

  1. 去使用SlimDx
  2. SharpDx
  3. DirectX

您可以使用這些庫中的任何一個並獲取輸入。對不起,但它不像改變陳述那麼簡單。

+0

好的,那麼其中一個看起來如何?所有我看過的遊戲杆代碼解釋瞭如何實例化它們,但不知道如何使用它們進行速度或方向控制。 – Flotolk

+0

@Flotolk首先,你需要看看這些,看看你想使用哪一個,然後我給了鏈接,以便你仔細看看工作示例。 – Ehsan