2017-02-21 6 views
0

私は前進してカメラを左に動かすとカメラの方向に移動しません左に進むが、これを修正する方法は何か?プレイヤーの動きスクリプト「w」を押したままカメラを動かすと、カメラが向いている場所にプレーヤーが移動しません

public float movementspeed = 5.0F; 

// Use this for initialization 
void Start() { 
    Cursor.lockState = CursorLockMode.Locked; 
} 

// Update is called once per frame 
void Update() { 

    float translation = Input.GetAxis("Vertical") * movementspeed; 
    float straffe = Input.GetAxis("Horizontal") * movementspeed; 
    translation *= Time.deltaTime; 
    straffe *= Time.deltaTime; 

    transform.Translate(straffe, 0, translation); 
} 

}

とカメラビュースクリプト

public float xMoveThreshold = 1000.0f; 
public float yMoveThreshold = 1000.0f; 

public float yMaxLimit = 50.0f; 
public float yMinLimit = -50.0f; 


float yRotCounter = 0.0f; 
float xRotCounter = 0.0f; 

Transform player; 

void Start() 
{ 
    player = Camera.main.transform; 
} 

// Update is called once per frame 
void Update() 
{ 
    xRotCounter += Input.GetAxis("Mouse X") * xMoveThreshold * Time.deltaTime; 
    yRotCounter += Input.GetAxis("Mouse Y") * yMoveThreshold * Time.deltaTime; 
    yRotCounter = Mathf.Clamp(yRotCounter, yMinLimit, yMaxLimit); 
    //xRotCounter = xRotCounter % 360;//Optional 
    player.localEulerAngles = new Vector3(-yRotCounter, xRotCounter, 0); 
} 

}

Picture 1

Picture 2

+0

私はカメラに最初のスクリプトを、2番目のスクリプトの空のゲームオブジェクトを添付しました。私はカメラを動かすことができた。動きはいいです。常にカメラが向いている方向に移動します。あなたが持っている問題は何ですか? – Programmer

+0

私は左を見て、「w」を押すと左にならないと言います。動きの方向はカメラによって変わらず、私が前向きに「w」を押してカメラに触れないように、それが特定のやり方で進むかどうかは関係ありませんまっすぐですが、カメラを回して 'w'を押すと後方に移動します – Johnny

+1

あなたの質問で更新した画像では、** GameObjectの下にカメラを置かないでください。それは逆のやり方です。そのプレーヤーをカメラの子にして、両方のスクリプトを現在のPlayerの親オブジェクトであるカメラに添付します。 – Programmer

答えて

0

これを行うには、カメラ変換を使用する必要があります。何かのように:

関連する問題