首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Unity3D控制无人机

在Unity3D控制无人机
EN

Code Review用户
提问于 2016-03-14 18:41:24
回答 1查看 3.8K关注 0票数 2

我目前正在开发一个名为“无人机!攻击!”的游戏,我需要一个玩家可以使用的无人机控制器。它目前非常简单,只支持少量的操作,例如:

  • 基本动作(前、后、左、右、上、下)
  • FPS摄像机控制
  • 摄像机范围(例如,将FOV从较高的值更改为较低的值,以获得“放大”效果)。

我想知道的是:

  • 这三个脚本应该合并成一个主PlayerController.cs文件吗?
  • 是否有一种方法可以清除PlayerMovementController.FixedUpdate中的键盘输入签入,同时确保诸如扫射之类的东西仍然有效?
  • 有没有办法可以缩短相机缩放代码?感觉太长了。

PlayerCameraController.cs

代码语言:javascript
复制
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);
        }
    }
}

PlayerMovementController.cs

代码语言:javascript
复制
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);
            }
        }
    }
}

PlayerZoomCamera.cs

代码语言:javascript
复制
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;
            }
        }
    }
}
EN

回答 1

Code Review用户

回答已采纳

发布于 2016-04-06 16:07:13

这三个脚本应该合并成一个主PlayerController.cs文件吗?

这一切都取决于你想用它做什么,所以基本上你的项目是如何组织的。它可能值得分离输入,创建一个新的类处理输入,只需调用适当的函数在其他(相机,播放器)类。

如果保持原样,似乎应该将PlayerZoomCamera.cs和PlayerCameraController.cs合并在一起。

是否有一种方法可以清除PlayerMovementController.FixedUpdate中的键盘输入签入,同时确保诸如扫射之类的操作仍然有效?

如果您不能向前/向后按,那么左/右同时可以添加一些“forward‘s”。

你可以通过以下方式清理每堂课:

  • 移除当前放在变量之前的this.
  • publicStart()Update()函数中删除,除非需要从其他脚本中调用它们
  • 声明变量时删除private,因为它们默认是私有的

有没有办法可以缩短相机缩放代码?感觉太长了。

代码语言:javascript
复制
void Update()
{
    if( Input.GetMouseButtonDown( zoomButton ) )
    {
        isCameraZoomed = !isCameraZoomed;
        playerCamera.fieldOfView = ( isCameraZoomed ? ReducedFOV : StandardFOV );
    }
}

我还建议对PlayerCameraController.cs做一些修改

代码语言:javascript
复制
private float xRotation = 0.0f;
private float yRotation = 0.0f;

代码语言:javascript
复制
Vector3 newRotation = new Vector3();

然后,代替

代码语言:javascript
复制
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");

你可以直接写

代码语言:javascript
复制
newRotation.x += VerticalInversion * VerticalSensitivity * Input.GetAxis("Mouse Y");
newRotation.y += HorizontalSensitivity * Input.GetAxis("Mouse X");
transform.eulerAngles = newRotation;

这是一种很好的、干净的变换位置和旋转的方法。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/122844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档