Unity の宇宙船コントローラー

このチュートリアルでは、Unity で宇宙船コントローラーを作成する方法を説明します。

さぁ、始めよう!

ステップ

  • spaceship モデルをシーンに配置します。

  • 新しいゲームオブジェクトを作成し、それを呼び出します "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" オブジェクトにアタッチします
  • 新しいゲームオブジェクトを作成し、"CameraPosition" という名前を付け、"Spaceship" オブジェクト内に移動します。
  • メイン カメラを "CameraPosition" オブジェクト内に移動し、その位置を (0, 0, 0) に変更します。
  • 満足のいく結果が得られるまで、"CameraPosition" オブジェクトの位置を微調整します。

  • メインカメラを "Spaceship" オブジェクトの外側に移動します
  • SC_SpaceshipController で、カメラ位置、メインカメラ、および宇宙船ルート (これは宇宙船モデルの変換である必要があります) 変数を割り当てます。

  • 新しい UI キャンバスを作成します (ゲームオブジェクト -> UI -> キャンバス)
  • Canvas オブジェクトを右クリック -> UI -> 画像
  • 新しい画像の配置を左上に変更します

  • 以下のスプライトを画像に割り当てます

SF十字線

  • 最後に、新しく作成した画像を SC_SpaceshipController の Crosshair Texture に割り当てます。

宇宙船コントローラーの準備が整いました。マウスを使用して周囲を見回し、A/D を使用して Z 軸に沿って回転し、マウスの右ボタンを使用して加速します。

おすすめの記事
Unity用ヘリコプターコントローラー
Unity の 2D プラットフォーマー キャラクター コントローラーにダブル ジャンプ サポートを追加する
Unityでクレーンコントロールを作る方法
Unity用カーコントローラー
キャラクター コントローラー Unity でリジッドボディをプッシュする機能を追加する方法
Unity用飛行機コントローラー
Unity でパルクール システムを実装する