0
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?

Cinemachine 一部座標を固定して他は追従させる[Unity]

Posted at

前置き

既存のAPIに機能がない時に、1から自分で作るのではなく、少し機能性を持たせることで想定外のリスクを避けるのたまにあると思います。この記事はそれです。

前提

  1. 2.5Dゲームを開発している
  2. x座標は対象gameObjectを追従させたい
  3. y座標とz座標は固定したい
  4. Unity6を利用している

対処法

Cinemachine Cameraを選択する
→Position ControlではFollow > Lock To Target On Assignを選択
→以下のコード入りのファイルを作成する
→LockAndYAndZAxisで検索してそのcomponentを貼り付ける
→終わり!

LockYAndZAxis.cs
/*
 * Copyright (c) DeepNapEngine
 * https://github.com/TrueRyoB
 */
using UnityEngine;
using Unity.Cinemachine;

namespace PoseQ.Extensions.RhyRhyDaDa
{
    [ExecuteAlways]
    [SaveDuringPlay]
    [AddComponentMenu("Cinemachine/Custom/LockYAndZAxis")]
    public class LockYAndZAxis : CinemachineExtension
    {
        [Header("固定したいYとZの値")]
        public float fixedY = 1f;
        public float fixedZ = 10f;
    
        protected override void PostPipelineStageCallback(
            CinemachineVirtualCameraBase vcam,
            CinemachineCore.Stage stage,
            ref CameraState state,
            float deltaTime)
        {
            if (stage == CinemachineCore.Stage.Body)
            {
                Vector3 pos = state.RawPosition;
                pos.y = fixedY;
                pos.z = fixedZ;
                state.RawPosition = pos;
            }
        }
    }
}

まとめ

将来的に導入予定のImpulse Listnerとの衝突が怖いなと思っています。
またFPSがかなり落ちたのも懸念点です。
飽くまで座標追跡と振動のみに機能性を絞るなら、コード設計n自作もあながち悪くないのではと考え直しています。

0
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
0
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?