2016-04-03 39 views
0

我只是在研究Unity5,因爲它是我的學校項目。他們告訴我做一個fps遊戲,我嘗試製作一個fps,讓相機按Q和E旋轉,但我無法用鼠標移動相機。而當我想輸入鼠標時,我根本無法移動我的fps字符。在這裏我的代碼:我如何通過鼠標在鼠標中查看鼠標在fps中的統一性5

using UnityEngine; 

public class Player : MonoBehaviour { 

private MazeCell currentCell; 

private MazeDirection currentDirection; 

public void SetLocation (MazeCell cell) { 
    if (currentCell != null) { 
     currentCell.OnPlayerExited(); 
    } 
    currentCell = cell; 
    transform.localPosition = cell.transform.localPosition; 
    currentCell.OnPlayerEntered(); 
} 

private void Move (MazeDirection direction) { 
    MazeCellEdge edge = currentCell.GetEdge(direction); 
    if (edge is MazePassage) { 
     SetLocation(edge.otherCell); 
    } 
} 

private void Look (MazeDirection direction) { 
    transform.localRotation = direction.ToRotation(); 
    currentDirection = direction; 
} 

private void Update() { 



    if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) { 
     Move(currentDirection); 
    } 
    if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) { 
     Move(currentDirection.GetNextClockwise()); 
    } 
    if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) { 
     Move(currentDirection.GetOpposite()); 
    } 
    if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) { 
     Move(currentDirection.GetNextCounterclockwise()); 
    } 
    if (Input.GetKeyDown(KeyCode.Q)) { 
     Look(currentDirection.GetNextCounterclockwise()); 
    } 
    if (Input.GetKeyDown(KeyCode.E)) { 
     Look(currentDirection.GetNextClockwise()); 
    } 
} 

}

回答

0

首先你應該remoove了「否則,如果」在你的更新功能,並用一個簡單的「如果」取代他們。 因爲會發生什麼情況是,只要if語句中的一個爲真,就會跳過以下函數。 應該解決它。

+0

反正我假設你在編碼所以這裏一個建議新:永遠不要使用esle如果你的代碼只是使用if {}否則{如果{...}}。它使事情變得更簡單:D – MrSunshine

+0

然後呢?先生,請告訴我更多 –

+0

sry正在緊迫進入xD – MrSunshine

0

試試這個:

public class Player : MonoBehaviour { 
//Position of mouse since last change in viewdirection 
private float mousePosLast; 

//Tollerance of mouse input 
//this is optional but makes the the input not that sensitive 
public float mouseTollerance; 

//sets the correct position of mouse on start 
public void Start() { 
    mousePosLast = Input.mousePosition.x; 
} 


public void Update() { 
    /* 
    ...other update code... 
    */ 

    if (Input.GetKeyDown(KeyCode.Q)) { 
       Look(currentDirection.GetNextCounterclockwise()); 
     } 
     if (Input.GetKeyDown(KeyCode.E)) { 
      Look(currentDirection.GetNextClockwise()); 
     } 

    //check if change in mouse position is big enougth to trigger camera rotation 
    if(Mathf.Abs(Input.mousePosition.x - mousePosLast) > mouseTollerance){ 
     //check whether to turn right or left 
     if(Input.mousePosition.x - mousePosLast > 0){ 
      Look(currentDirection.GetNextCounterclockwise()); 
     }else{ 
      Look(currentDirection.GetNextClockwise()); 
     } 
    } 
} 

private void Look (MazeDirection direction) { 
     transform.localRotation = direction.ToRotation(); 
     currentDirection = direction; 

    //set mousePosLast to current mouseposition on every change -> avoids strange effects 
    mousePosLast = Input.mousePosition.x; 
} 

}

+0

哇,它讓我看穿遊戲的牆壁,但無論如何感謝。 –

+0

也許你應該看看你的整個代碼..可能有一些問題不是由你的Player類直接引起的。 – MrSunshine

+0

啊不,這是我現在所有的代碼 –