0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unity オブジェクトを周回する機能実装

Posted at

RendererTexture記事の続き

「あるオブジェクトの周りをカメラが周回し、そのカメラ映像をモニターのオブジェクトを映したい」ということがありました。

主に対応する点が以下3点。

  1. Render Textureを用いてカメラ映像をマテリアルとして投影
  2. カメラに映したくないものを描画対象から除く
  3. オブジェクトを中心に周回するカメラを設定

本記事は3.の内容です。

結論、以下のコードでミニマムな実装ができます。

using UnityEngine;

namespace Unity.FantasyKingdom
{
	public class NewMonoBehaviourScript : MonoBehaviour
	{
		[SerializeField]
		Transform target;
		[SerializeField]
		float rotationSpeed = 20.0f;

		void Update()
		{
			// ターゲットの原点を中心にカメラを回転
			transform.RotateAround(target.position, Vector3.up, rotationSpeed * Time.deltaTime);

			// 常にターゲットの方向を向く
			transform.LookAt(target);
		}
	}
}

Transform.RotateAroundとは

RotateAround (Vector3 point, Vector3 axis, float angle)

point: 回転の中心となる点(例: target.position)。
axis: 回転の軸(例: Vector3.up はY軸)。
angle: 回転させる角度(度単位)。

今回の実装では、「あるオブジェクト(point)の周りをカメラ(transform)がY軸回り(axis)に20.0°/sで(angle)回転する」という引数を設定しています。

Transform.LookAtとは

public void LookAt (Transform target);

transformのオブジェクトがtargetの方向を常に向きます。

(Screen Recording 2024-12-25 at 18.40.48)

ゲームでは自動ホーミング、プレゼンではシネマティックな視線誘導に使えますね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?