簡體   English   中英

如何在Unity 5中通過鼠標以fps方式查看相機的fps

[英]How can I mouse camera view by mouse for fps in unity 5

我正在研究Unity5,因為這是我的學校項目。 他們告訴我做一個fps游戲,我嘗試做一個fps,使照相機旋轉Q和E,但我不能用鼠標移動照相機。 當我想輸入mouse時,我根本無法移動fps字符。 這是我的代碼:

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());
    }
}

}

首先,您應該刪除更新功能中的“ else if”,然后將其替換為簡單的“ if”。 因為發生的情況是,只要其中一個if語句為true,就會跳過以下函數。 那應該解決它。

嘗試這個:

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;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM