ブログ

(Unity)フライトシューティング;360度方向転換

(構成&設定)


(スクリプト)

(1)キーボードで操作する場合

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

public class AirController : MonoBehaviour
{
    private float miuTurnInputValue;
    private Rigidbody miuRb;
    private float miuNoseInputValue;

    private void Start()
    {
        miuRb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        // 前進は自動
        transform.Translate(0f, 0f, 3f * Time.deltaTime);

        // 旋回
        miuTurnInputValue = Input.GetAxis("Horizontal");
        float turn = miuTurnInputValue * 100 * Time.deltaTime;
        Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
        miuRb.MoveRotation(miuRb.rotation * turnRotation);

        // 機首(上昇、下降)
        miuNoseInputValue = Input.GetAxis("Vertical");
        float noseTurn = miuNoseInputValue * 30 * Time.deltaTime;
        Quaternion turnNoseRotation = Quaternion.Euler(noseTurn, 0, 0);
        miuRb.MoveRotation(miuRb.rotation * turnNoseRotation);
    }
}

(2)マウスで操作する場合

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

public class AirController : MonoBehaviour
{
    private float miuTurnInputValue;
    private Rigidbody miuRb;
    private float miuNoseInputValue;

    private void Start()
    {
        miuRb = GetComponent&lt;Rigidbody&gt;();
    }

    void Update()
    {
        // 前進は自動
        transform.Translate(0f, 0f, 3f * Time.deltaTime);

        // 旋回
        miuTurnInputValue = Input.GetAxis("Mouse X");
        float turn = miuTurnInputValue * 100 * Time.deltaTime;
        Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
        miuRb.MoveRotation(miuRb.rotation * turnRotation);

        // 機首(上昇、下降)
        miuNoseInputValue = Input.GetAxis("Mouse Y");
        float noseTurn = miuNoseInputValue * 30 * Time.deltaTime;
        Quaternion turnNoseRotation = Quaternion.Euler(noseTurn, 0, 0);
        miuRb.MoveRotation(miuRb.rotation * turnNoseRotation);
    }
}