我只是在研究Unity5,因为这是我的学校项目。他们告诉我做一个fps游戏,我尝试制作一个fps,让相机按Q和E旋转,但我不能用鼠标移动相机。当我想输入鼠标时,我根本无法移动我的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());
}
}
}
答案 0 :(得分:0)
首先,您应该更新更新功能中的“else if”并将其替换为简单的“if”。 因为只要你的一个if语句为真,就会跳过以下函数。 应该解决它。
答案 1 :(得分: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;
}
}