2014-10-27 37 views
0

我正在創造一個遊戲,我將是一個立方體。但是當我在X軸上移動我的立方體時,Z軸會改變。 我也阻止了X,Y,Z旋轉。 密碼:統一 - 當我在X軸上移動一個對象時,爲什麼Z軸會改變?

using UnityEngine; 
using System.Collections; 

public class CubeControl : MonoBehaviour { 
    private Vector3 input; 

    void Update() { 
     if(Input.GetKey(KeyCode.D)){ 
      input = new Vector3(25, 0, 0); 
      rigidbody.AddForce(input); 
     } 
     if(Input.GetKey(KeyCode.A)){ 
      input = new Vector3(-25, 0, 0); 
      rigidbody.AddForce(input); 
     } 
     if(Input.GetKey(KeyCode.W)){ 
      input = new Vector3(0, 0, 25); 
      rigidbody.AddForce(input); 
     } 
     if(Input.GetKey(KeyCode.S)){ 
      input = new Vector3(0, 0, -25); 
      rigidbody.AddForce(input); 
     } 
    } 
} 
+0

嘿,分享您的檢查員值立方體的快照。 此外,使用FixedUpdate代替Update方法會更好,因爲您試圖應用恆定的力量。 – Billatron 2014-10-27 18:11:54

+0

@Billatron http://i.imgur.com/r7LFbi8.png – PanaitStudio 2014-10-27 18:30:36

+0

極有可能是它引起的重力下降原因。 – Billatron 2014-10-27 18:37:05

回答

1

它看起來像你應用在世界空間的運動。您可能希望將其應用於對象空間。所以你需要旋轉由對象變換生成的矢量。喜歡的東西:

Vector3 input = new Vector3(25,0,0); 
input = this.transform.rotation * input; 
rigidbody.AddForce(input); 

此外,一對夫婦的事情,讓您的生活更輕鬆:

  • 查找到Input.GetAxis()東西做輸入。可以減輕你的生活,以便後續設置/配置。

  • 我喜歡使用Vector3.up/Vector3.left/Vector3.forward等並乘以標量。當想要繁殖時讓事情看起來更直觀。