Unity 中的宇宙飞船控制器

在本教程中,我将展示如何在 Unity 中制作宇宙飞船控制器。

让我们开始!

脚步

  • 将 spaceship 模型放入场景中

  • 创建一个新的GameObject,调用它 "Spaceship"
  • 将宇宙飞船模型移动到 "Spaceship" 对象内并将其位置更改为 (0, 0, 0)
  • 创建 一个新脚本,将其命名为 "SC_SpaceshipController" 并将以下代码粘贴到其中:

SC_SpaceshipController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]

public class SC_SpaceshipController : MonoBehaviour
{
    public float normalSpeed = 25f;
    public float accelerationSpeed = 45f;
    public Transform cameraPosition;
    public Camera mainCamera;
    public Transform spaceshipRoot;
    public float rotationSpeed = 2.0f;
    public float cameraSmooth = 4f;
    public RectTransform crosshairTexture;

    float speed;
    Rigidbody r;
    Quaternion lookRotation;
    float rotationZ = 0;
    float mouseXSmooth = 0;
    float mouseYSmooth = 0;
    Vector3 defaultShipRotation;

    // Start is called before the first frame update
    void Start()
    {
        r = GetComponent<Rigidbody>();
        r.useGravity = false;
        lookRotation = transform.rotation;
        defaultShipRotation = spaceshipRoot.localEulerAngles;
        rotationZ = defaultShipRotation.z;

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void FixedUpdate()
    {
        //Press Right Mouse Button to accelerate
        if (Input.GetMouseButton(1))
        {
            speed = Mathf.Lerp(speed, accelerationSpeed, Time.deltaTime * 3);
        }
        else
        {
            speed = Mathf.Lerp(speed, normalSpeed, Time.deltaTime * 10);
        }

        //Set moveDirection to the vertical axis (up and down keys) * speed
        Vector3 moveDirection = new Vector3(0, 0, speed);
        //Transform the vector3 to local space
        moveDirection = transform.TransformDirection(moveDirection);
        //Set the velocity, so you can move
        r.velocity = new Vector3(moveDirection.x, moveDirection.y, moveDirection.z);

        //Camera follow
        mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, cameraPosition.position, Time.deltaTime * cameraSmooth);
        mainCamera.transform.rotation = Quaternion.Lerp(mainCamera.transform.rotation, cameraPosition.rotation, Time.deltaTime * cameraSmooth);

        //Rotation
        float rotationZTmp = 0;
        if (Input.GetKey(KeyCode.A))
        {
            rotationZTmp = 1;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            rotationZTmp = -1;
        }
        mouseXSmooth = Mathf.Lerp(mouseXSmooth, Input.GetAxis("Mouse X") * rotationSpeed, Time.deltaTime * cameraSmooth);
        mouseYSmooth = Mathf.Lerp(mouseYSmooth, Input.GetAxis("Mouse Y") * rotationSpeed, Time.deltaTime * cameraSmooth);
        Quaternion localRotation = Quaternion.Euler(-mouseYSmooth, mouseXSmooth, rotationZTmp * rotationSpeed);
        lookRotation = lookRotation * localRotation;
        transform.rotation = lookRotation;
        rotationZ -= mouseXSmooth;
        rotationZ = Mathf.Clamp(rotationZ, -45, 45);
        spaceshipRoot.transform.localEulerAngles = new Vector3(defaultShipRotation.x, defaultShipRotation.y, rotationZ);
        rotationZ = Mathf.Lerp(rotationZ, defaultShipRotation.z, Time.deltaTime * cameraSmooth);

        //Update crosshair texture
        if (crosshairTexture)
        {
            crosshairTexture.position = mainCamera.WorldToScreenPoint(transform.position + transform.forward * 100);
        }
    }
}
  • SC_SpaceshipController 脚本附加到 "Spaceship" 对象
  • 创建一个新的 GameObject,将其命名为 "CameraPosition" 并将其移动到 "Spaceship" 对象内
  • 将主摄像机移动到 "CameraPosition" 对象内并将其位置更改为 (0, 0, 0)
  • 调整 "CameraPosition" 对象位置,直到您对结果满意为止

  • 将主摄像机移到 "Spaceship" 对象之外
  • 在 SC_SpaceshipController 中分配相机位置、主相机和宇宙飞船根(这应该是宇宙飞船模型的变换)变量

  • 创建一个新的 UI Canvas(游戏对象 -> UI -> Canvas)
  • 右键单击 Canvas 对象 -> UI -> 图像
  • 将新图像的对齐方式更改为左上角

  • 将下面的精灵分配给图像

科幻十字线

  • 最后,将新创建的图像分配给 SC_SpaceshipController 中的十字线纹理

宇宙飞船控制器已准备就绪。使用鼠标环顾四周,使用A/D沿Z轴旋转,使用鼠标右键加速。