我目前正在开发一个名为“无人机!攻击!”的游戏,我需要一个玩家可以使用的无人机控制器。它目前非常简单,只支持少量的操作,例如:
我想知道的是:
PlayerController.cs文件吗?PlayerMovementController.FixedUpdate中的键盘输入签入,同时确保诸如扫射之类的东西仍然有效?using UnityEngine;
namespace DronesAttack
{
public class PlayerCameraController : MonoBehaviour
{
public float HorizontalSensitivity = 3.5f;
public float VerticalSensitivity = 3.5f;
public int VerticalInversion = -1;
private float xRotation = 0.0f;
private float yRotation = 0.0f;
public void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
public void FixedUpdate()
{
this.xRotation += this.VerticalInversion * this.VerticalSensitivity * Input.GetAxis("Mouse Y");
this.yRotation += this.HorizontalSensitivity * Input.GetAxis("Mouse X");
this.transform.eulerAngles = new Vector3(this.xRotation, this.yRotation, 0);
}
}
}using UnityEngine;
using System.Collections.Generic;
namespace DronesAttack
{
public class PlayerMovementController : MonoBehaviour
{
public float ForwardMovementSpeed = 0.25f;
public float SideMovementSpeed = 0.1f;
public float VerticalMovementSpeed = 0.125f;
private Dictionary<string, KeyCode> movementKeyBindings = new Dictionary<string, KeyCode>()
{
{ "FORWARD", KeyCode.W },
{ "BACKWARD", KeyCode.S },
{ "LEFT", KeyCode.A },
{ "RIGHT", KeyCode.D },
{ "UP", KeyCode.Space },
{ "DOWN", KeyCode.LeftShift }
};
public void FixedUpdate()
{
if(Input.GetKey(this.movementKeyBindings["FORWARD"]))
{
this.transform.position += new Vector3(
this.transform.forward.x * this.ForwardMovementSpeed,
0,
this.transform.forward.z * this.ForwardMovementSpeed
);
}
if(Input.GetKey(this.movementKeyBindings["BACKWARD"]))
{
this.transform.position += new Vector3(
this.transform.forward.x * (-this.ForwardMovementSpeed / 1.95f),
0,
this.transform.forward.z * (-this.ForwardMovementSpeed / 1.95f)
);
}
if(Input.GetKey(this.movementKeyBindings["LEFT"]))
{
this.transform.Translate(Vector3.left * this.SideMovementSpeed);
}
if(Input.GetKey(this.movementKeyBindings["RIGHT"]))
{
this.transform.Translate(Vector3.right * this.SideMovementSpeed);
}
if(Input.GetKey(this.movementKeyBindings["UP"]))
{
this.transform.Translate(Vector3.up * this.VerticalMovementSpeed);
}
if(Input.GetKey(this.movementKeyBindings["DOWN"]))
{
this.transform.Translate(Vector3.down * this.VerticalMovementSpeed);
}
}
}
}using UnityEngine;
namespace DronesAttack
{
public class PlayerZoomCamera : MonoBehaviour
{
public float StandardFOV = 62.5f;
public float ReducedFOV = 32.5f;
private bool isCameraZoomed = false;
private int zoomButton = 1;
private Camera playerCamera;
public void Start()
{
this.playerCamera = GetComponent<Camera>();
}
public void Update()
{
if(Input.GetMouseButtonDown(this.zoomButton) && !this.isCameraZoomed)
{
this.playerCamera.fieldOfView = this.ReducedFOV;
this.isCameraZoomed = true;
}
else if(Input.GetMouseButtonDown(this.zoomButton) && this.isCameraZoomed)
{
this.playerCamera.fieldOfView = this.StandardFOV;
this.isCameraZoomed = false;
}
}
}
}发布于 2016-04-06 16:07:13
这三个脚本应该合并成一个主PlayerController.cs文件吗?
这一切都取决于你想用它做什么,所以基本上你的项目是如何组织的。它可能值得分离输入,创建一个新的类处理输入,只需调用适当的函数在其他(相机,播放器)类。
如果保持原样,似乎应该将PlayerZoomCamera.cs和PlayerCameraController.cs合并在一起。
是否有一种方法可以清除PlayerMovementController.FixedUpdate中的键盘输入签入,同时确保诸如扫射之类的操作仍然有效?
如果您不能向前/向后按,那么左/右同时可以添加一些“forward‘s”。
你可以通过以下方式清理每堂课:
this.,public从Start()和Update()函数中删除,除非需要从其他脚本中调用它们private,因为它们默认是私有的有没有办法可以缩短相机缩放代码?感觉太长了。
void Update()
{
if( Input.GetMouseButtonDown( zoomButton ) )
{
isCameraZoomed = !isCameraZoomed;
playerCamera.fieldOfView = ( isCameraZoomed ? ReducedFOV : StandardFOV );
}
}我还建议对PlayerCameraController.cs做一些修改
private float xRotation = 0.0f;
private float yRotation = 0.0f;至
Vector3 newRotation = new Vector3();然后,代替
this.transform.eulerAngles = new Vector3(this.xRotation, this.yRotation, 0);
this.xRotation += this.VerticalInversion * this.VerticalSensitivity * Input.GetAxis("Mouse Y");
this.yRotation += this.HorizontalSensitivity * Input.GetAxis("Mouse X");你可以直接写
newRotation.x += VerticalInversion * VerticalSensitivity * Input.GetAxis("Mouse Y");
newRotation.y += HorizontalSensitivity * Input.GetAxis("Mouse X");
transform.eulerAngles = newRotation;这是一种很好的、干净的变换位置和旋转的方法。
https://codereview.stackexchange.com/questions/122844
复制相似问题