1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

肩越しカメラの作成

Last updated at Posted at 2021-07-26

PlayerAnimatorを作成し、BlendTreeを作成する
スクリーンショット 2021-04-27 123203.png
FrontとSideのパラメーターをFloatで作成する
スクリーンショット 2021-04-27 123536.png
BlendTreeにMotionを設定する
スクリーンショット 2021-04-27 123603.png
スクリーンショット 2021-04-27 124347.png
BlendType、Parametersを設定し、ComputePositionsをVelocityXYにする
スクリーンショット 2021-04-27 124523.png
再生して動作を確認する
スクリーンショット 2021-04-27 125400.png
シネマシーンの実装

CinemaChineをインポートする
スクリーンショット 2021-04-27 220702.png
CinemaChineのCreateVirtualを選択する
スクリーンショット 2021-04-27 220754.png
CMオブジェクトを選択し、3rdPersonFollowを選択、AimはNothingにする
スクリーンショット 2021-04-27 220920.png
Playerオブジェクトの中にEyeオブジェクトを作成する
スクリーンショット 2021-04-27 221409.png
CMオブジェクトのDistanceを調整する
スクリーンショット 2021-04-27 221628.png
※再生するとPlayerの位置がズレていることがあるのでanimationを開き、Idleなど確認用のアニメーションに設定して鍵をしておくと調整間違いを防止できる
スクリーンショット 2021-04-27 221857.png
カメラ位置を調整する
スクリーンショット 2021-04-27 222455.png
Eyeのポジションを調整する
スクリーンショット 2021-04-27 222526.png

Characterスクリプトを作成する

Character
using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Character : MonoBehaviour
{
    Animator animator;

    [SerializeField] float speed;

    [Header("Aim")]
    [SerializeField] Transform eye;
    [SerializeField] AxisState Vertical;
    [SerializeField] AxisState Horizontal;

    static int hashFront = Animator.StringToHash("Front");
    static int hashSide = Animator.StringToHash("Side");

    void Awake()
    {
        //コンポーネントと関連付け
        TryGetComponent(out animator);
    }

    void Start()
    {
        //カーソルを中央に固定(マウスを動かすゲーム用)
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        //入力を取得
        var inputX = Input.GetAxis("Horizontal");
        var inputY = Input.GetAxis("Vertical");
        var leftStick = new Vector3(inputX, 0, inputY).normalized;
        Horizontal.Update(Time.deltaTime);
        Vertical.Update(Time.deltaTime);

        //移動速度を計算
        var velocity = speed * leftStick;

        //向きを更新
        var horizontalRotation = Quaternion.AngleAxis(Horizontal.Value, Vector3.up);
        var verticalRotation = Quaternion.AngleAxis(Vertical.Value, Vector3.right);
        transform.rotation = horizontalRotation;
        eye.localRotation = verticalRotation;

        //Animatorに反映
        animator.SetFloat(hashFront, velocity.z, 0.1f, Time.deltaTime);
        animator.SetFloat(hashSide, velocity.x, 0.1f, Time.deltaTime);
    }
}

VerticalとHorizontalの各値を設定する
スクリーンショット 2021-04-27 225535.png
スクリーンショット 2021-04-27 225706.png
Eyeをアタッチする
スクリーンショット 2021-04-27 230157.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?