2
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.

【Unity】Cinemachine で即座にバーチャルカメラの位置にリセットする

Posted at

Unity が所持する強力なカメラシステム Cinemachine ですが、位置の初期化でつまづいてしまったので備忘録です。

確認バージョン

Unity 2020.3.13f1
Cinemachine 2.6.10

はじめに

Cinemachine はカメラ本体とは別にバーチャルカメラという仮想の概念があり、このバーチャルカメラ間をカメラ本体が渡り歩いていくイメージで、効率よく滑らかなカメラワークを実現できます。

ですのでバーチャルカメラをワープ1させたとしても、カメラ本体はそれに遅れる形で滑らかにバーチャルカメラの位置まで移動していきます。
ですが、配置、リスポーン、テレポートなど、補間などせずすぐにバーチャルカメラの位置に合致してほしいシチュエーションはありますよね。
たとえカメラ本体(CinemachineBrain)の transform を直接設定しても、内部の補間計算は続いているため、結局補間途中の位置に戻されてしまいうまくいきません。
今回はカメラ本体をすぐにバーチャルカメラの位置に飛ばすやり方のメモになります。

方法

要するに補間処理を打ち切ってやれば OK でして、やり方はバーチャルカメラの位置を変える処理の直後で CinemachineVirtualCameraBase.PreviousStateIsValid を false に設定するだけです。

    _cinemachineVirtualCameraBase.PreviousStateIsValid = false;

私は CinemachineBrain 側からアプローチしたかったので、次のように使いました。

    (_cinemachineBrain.ActiveVirtualCamera as CinemachineVirtualCameraBase).PreviousStateIsValid = false;

~~ちょっと名前が直感的でないため意図が読みにくいコードですが、~~これでやりたいことが達成できましたので、同じようなことでつまづいている方の参考になれば幸いです。

参考

  1. CinemachineVirtualCamera の Follow や LookAt のターゲットを変える、もしくは transform を直接設定するなど。

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