Unity:三人称カメラの回転するやつとしないやつメモ

もしなにかあればTwitterで教えて欲しいです。
Twitter:@ritz_prgrm
マウスに合わせてプレイヤーが回転するかしないかをいい感じにしたかった。
まだ設定してないけどボタンを押せばカメラの仕様を変えることもできる。
いずれ一人称も作りたい。一人称に関しては場所を変えるだけだけど。

PlayerCamera
プレイヤーになるオブジェクトの子オブジェクトのカメラにあてる。
Player
---Camera ←これにあてる。
インスペクタ上でカメラの挙動を切り替えたかったのでenumで切り替えている。
やろうと思えばInputで切り替えることもできる。
Type.oneでカメラの回転に合わせてプレイヤーが回転する。
Type.twoでプレイヤーをなめまわすようにカメラが回せるようになる。

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

public class PlayerCamera : MonoBehaviour
{
    public Transform VerticalRotation;
    public Transform HorizontalRotation;

    public float XRotation;
    public float YRotation;

    float distance = 2f;
    public enum Type
    {
        one,
        two
    }
    public Type CurrentType;
    // Start is called before the first frame update
    private void Awake()
    {
        if(VerticalRotation == null)
        {
            VerticalRotation = transform.parent;
        }
        if(HorizontalRotation == null)
        {
            HorizontalRotation = GetComponent();
        }
    }
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        XRotation = Input.GetAxis("Mouse X");
        YRotation = Input.GetAxis("Mouse Y");
        if (CurrentType == Type.one)
        {
            //ここをマイナスにするとマウスの上下と逆にカメラワークが動く
            VerticalRotation.transform.Rotate(0, XRotation, 0);
            //ここをプラスにするとマウスの左右と逆にカメラワークが動く
            HorizontalRotation.transform.Rotate(-YRotation, 0, 0);
        }
        else if(CurrentType == Type.two)
        {
            var look = VerticalRotation.position + Vector3.up;
            //上下だけかいてん制限
            transform.RotateAround(VerticalRotation.up, Vector3.up, XRotation);
            if (transform.forward.y > 0.8f && YRotation < 0)
            {
                YRotation = 0;
            }
            if (transform.forward.y < -0.8f && YRotation > 0)
            {
                YRotation = 0;
            }
            transform.RotateAround(VerticalRotation.right, Vector3.right, -YRotation);
            transform.position = look - transform.forward * distance;
            transform.LookAt(look);
        }
    }
}

あとはPlayerのUpdateとFixedUpdateでいい感じに使う。
Player(Update)

    void Update()
    {
        inputHorizontal = Input.GetAxisRaw("Horizontal");
        inputVertical = Input.GetAxisRaw("Vertical");
        if (Input.GetMouseButtonDown(0))
        {
            if (PlayerCamera.CurrentType == PlayerCamera.Type.two)
            {
                //カメラの方向に向かってプレイヤーが向く。
                transform.rotation = Quaternion.LookRotation(Vector3.Scale(PlayerCamera.transform.forward, new Vector3(1, 0, 1)).normalized);
                //プレイヤーが回転したぶんカメラも回転するのでもとにもどす。
                PlayerCamera.transform.rotation = transform.rotation;
            }
        }
    }

Player(FixedUpdate)
rigidBodyは別で宣言してるものだけどめんどくさいから割愛。一応プレイヤーのRigidBodyを指定。
これで歩けるようにはなるけど平面しか考えていないので空中での挙動がめちゃくちゃふわふわする。

    void FixedUpdate()
    {
        // カメラの方向から、XZ平面の単位ベクトルをもとめる
        Vector3 cameraForward = Vector3.Scale(PlayerCamera.transform.forward, new Vector3(1, 0, 1)).normalized;
        Vector3 cameraSide = Vector3.Scale(PlayerCamera.transform.right, new Vector3(1, 0, 1)).normalized;
        // 方向キーの入力値とカメラの向きから移動方向を決定
        Vector3 moveForward = cameraForward * inputVertical;
        Vector3 moveSide = cameraSide * inputHorizontal;
        rigidBody.velocity = rigidBody.velocity = (moveForward + moveSide) * moveSpeed;
        //空中では+ new Vector3(0, rigidBody.velocity.y, 0);で重力加算
        if (PlayerCamera.CurrentType == PlayerCamera.Type.one)
        {
            // キャラクターの向きを進行方向に
            if (moveForward != Vector3.zero && inputVertical > 0)
            {
                transform.rotation = Quaternion.LookRotation(moveForward);
            }
            else if (moveForward != Vector3.zero && inputVertical < 0)
            {
                transform.rotation = Quaternion.LookRotation(-moveForward);
            }

            if (moveSide != Vector3.zero)
            {
                transform.rotation = Quaternion.LookRotation(cameraForward);
            }

            if (moveForward == Vector3.zero && moveSide == Vector3.zero)
            {
                transform.rotation = Quaternion.LookRotation(cameraForward);
            }
        }
    }

カメラの切り替えをボタンでしたいときはEnumのこれ使ってます。
https://prgrm.work/archives/793

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA