Bộ điều khiển tàu vũ trụ trong Unity

Trong hướng dẫn này, tôi sẽ trình bày cách tạo bộ điều khiển tàu vũ trụ trong Unity.

Hãy bắt đầu nào!

bước

  • Đặt mô hình spaceship vào Cảnh của bạn

  • Tạo một GameObject mới, gọi nó là "Spaceship"
  • Di chuyển mô hình Tàu vũ trụ bên trong đối tượng "Spaceship" và thay đổi vị trí của nó thành (0, 0, 0)
  • Tạo một tập lệnh mới, gọi nó là "SC_SpaceshipController" và dán đoạn mã bên dưới vào bên trong nó:

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);
        }
    }
}
  • Đính kèm tập lệnh SC_SpaceshipController vào đối tượng "Spaceship"
  • Tạo một GameObject mới, gọi nó là "CameraPosition" và di chuyển nó vào bên trong đối tượng "Spaceship"
  • Di chuyển Camera chính bên trong đối tượng "CameraPosition" và thay đổi vị trí của nó thành (0, 0, 0)
  • Tinh chỉnh vị trí đối tượng "CameraPosition" cho đến khi bạn hài lòng với kết quả

  • Di chuyển Camera chính ra ngoài đối tượng "Spaceship"
  • Trong SC_SpaceshipController chỉ định các biến Vị trí camera, Camera chính và Root tàu vũ trụ (đây phải là một biến đổi của mô hình tàu vũ trụ)

  • Tạo Canvas UI mới (GameObject -> UI -> Canvas)
  • Nhấp chuột phải vào đối tượng Canvas -> UI -> Hình ảnh
  • Thay đổi căn chỉnh của hình ảnh mới ở trên cùng bên trái

  • Gán sprite bên dưới cho ảnh

đường chéo khoa học viễn tưởng

  • Cuối cùng, gán hình ảnh vừa tạo cho Crosshair Text trong SC_SpaceshipController

Bộ điều khiển tàu vũ trụ đã sẵn sàng. Sử dụng chuột để quan sát xung quanh, A/D để xoay dọc theo trục Z và Nút chuột phải để tăng tốc.

Bài viết được đề xuất
Thêm Cúi người vào Trình phát FPS trong Unity
Cần điều khiển nhập cảm ứng di động trong Unity
Thêm hỗ trợ nhảy đôi vào bộ điều khiển nhân vật nền tảng 2D trong Unity
Cách thực hiện điều khiển cần cẩu trong Unity
Bộ điều khiển nhân vật Cách thêm khả năng đẩy các vật cứng trong Unity
Triển khai hệ thống Parkour trong Unity
Tạo chuyển động của người chơi trong Unity