2016-11-17 20 views
2

我從Youtube Unity教程視頻複製了這段代碼,我無法弄清楚它有什麼問題。統一輪換不能在Y軸上工作

當我一起玩遊戲時,我試圖用我的鼠標查找它試圖阻止我這樣做。

using UnityEngine; 
using System.Collections; 

public class FirstPersonController : MonoBehaviour { 

    public float movementSpeed = 5.0f; 
    float verticalRotation = 0; 
    public float upDownRange = 60.0f; 

    // Use this for initialization 
    void Start() { 

    } 

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

     //Rotation 
     float rotLeftRight = Input.GetAxis("Mouse X"); 
     transform.Rotate(0, rotLeftRight, 0); 

     verticalRotation = Input.GetAxis("Mouse Y"); 
     verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange); 
     Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0); 

     //Movement 
     float forwardSpeed = Input.GetAxis ("Vertical") * movementSpeed; 
     float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed; 

     Vector3 speed = new Vector3 (sideSpeed, 0, forwardSpeed); 

     speed = transform.rotation * speed; 

     CharacterController cc = GetComponent<CharacterController>(); 

     cc.SimpleMove (speed); 
    } 
} 
+0

我想你最好問** ** Youtube Unity教程視頻**的作者。 http://stackoverflow.com/help/how-to-ask – zwcloud

+0

它只是我還是他從不分配Y旋轉? –

+0

@Wheremy如果你的問題已經得到解答,一定要接受幫助你的答案。 – Alox

回答

1

我認爲問題在於,您在verticalRotation中指定鼠標Y值而不是增加它。

試着這麼做:

float yAxis = Input.GetAxis("Mouse Y"); 
verticalRotation = Mathf.Clamp(verticalRotation + yAxis, -upDownRange, upDownRange);` 

也許你也應該有一個rotationSpeed像你movementSpeed。

+0

我不認爲這就是它的工作方式,如果X工作,那麼Y.Mathf.Clamp將夾住的值保持在其他兩個值之間,如果Y大於或小於60,它將鉗位到60這是Mathf.Clamp的唯一原因。 – Alox

1

如果這個腳本是相機本身然後:

這裏,嘗試用更簡單的東西一點點更換您的更新。

void Update() 
{ 

    //Rotation 

    float rotLeftRight = Input.GetAxis("Mouse X"); 
    //transform.Rotate(0, rotLeftRight, 0); 

    verticalRotation = Input.GetAxis("Mouse Y"); 
    verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange); 
    //Only add the rotation when both values are set so you don't overwrite the first one. 
    transform.Rotate(-verticalRotation, rotLeftRight, 0); 

    //Movement 
    float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed; 
    float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed; 

    Vector3 speed = new Vector3(sideSpeed, 0, forwardSpeed); 

    speed = transform.rotation * speed; 

    CharacterController cc = GetComponent<CharacterController>(); 

    cc.SimpleMove(speed); 
} 

我相信你的主要問題是,你是設置X時,然後一點點後,你會通過設置在Y的X設置爲0,因爲這是在更新則設置y爲0,這是看起來很緊張,最後都在0.