Raumschiff-Controller in Unity

In diesem Tutorial zeige ich, wie man einen Raumschiff-Controller in Unity baut.

Lass uns anfangen!

Schritte

  • Platzieren Sie das Modell Raumschiff in Ihrer Szene

  • Erstellen Sie ein neues GameObject und nennen Sie es "Spaceship"
  • Bewegen Sie das Raumschiffmodell innerhalb des "Spaceship"-Objekts und ändern Sie seine Position in (0, 0, 0).
  • Erstellen ein neues Skript, nennen Sie es "SC_SpaceshipController" und fügen Sie den folgenden Code darin ein:

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);
        }
    }
}
  • Hängen Sie das SC_SpaceshipController-Skript an das "Spaceship"-Objekt an
  • Erstellen Sie ein neues GameObject, nennen Sie es "CameraPosition" und verschieben Sie es innerhalb des "Spaceship"-Objekts
  • Bewegen Sie die Hauptkamera in das Objekt "CameraPosition" und ändern Sie ihre Position in (0, 0, 0).
  • Passen Sie die Objektposition "CameraPosition" an, bis Sie mit dem Ergebnis zufrieden sind

  • Bewegen Sie die Hauptkamera außerhalb des "Spaceship"-Objekts
  • Weisen Sie in SC_SpaceshipController die Variablen Kameraposition, Hauptkamera und Raumschiffwurzel (dies sollte eine Transformation eines Raumschiffmodells sein) zu

  • Erstellen Sie einen neuen UI-Canvas (GameObject -> UI -> Canvas)
  • Klicken Sie mit der rechten Maustaste auf das Canvas-Objekt -> Benutzeroberfläche -> Bild
  • Ändern Sie die Ausrichtung eines neuen Bildes nach oben links

  • Weisen Sie dem Bild das untenstehende Sprite zu

Science-Fiction-Fadenkreuz

  • Weisen Sie abschließend das neu erstellte Bild der Crosshair Texture in SC_SpaceshipController zu

Der Raumschiff-Controller ist fertig. Benutzen Sie die Maus, um sich umzusehen, A/D, um sich entlang der Z-Achse zu drehen, und die rechte Maustaste, um zu beschleunigen.

Empfohlene Artikel
Hubschrauber-Controller für Unity
Hinzufügen von Double Jump-Unterstützung zu einem 2D-Platformer-Charakter-Controller in Unity
So erstellen Sie eine Kransteuerung in Unity
Auto-Controller für Unity
Charakter-Controller So fügen Sie die Fähigkeit hinzu, starre Körper in Einheit zu schieben
Flugzeugcontroller für Unity
Implementierung des Parkour-Systems in Unity