【Unity】オブジェクトAの周りを廻るオブジェクトB
オブジェクトBがオブジェクトAの周りを等速で回転し、常にオブジェクトAを見ているスクリプトは以下のように作成できます。このスクリプトをオブジェクトBにアタッチします


using UnityEngine;
public class OrbitAround : MonoBehaviour
{
public Transform target; // オブジェクトAのTransform
public float orbitSpeed = 10f; // 回転速度
public float distance = 5f; // 回転半径
void Update()
{
// オブジェクトAを中心にオブジェクトBを回転させる
Orbit();
// オブジェクトBが常にオブジェクトAを向くようにする
LookAtTarget();
}
void Orbit()
{
// 時間に基づいてオブジェクトBの新しい位置を計算
float angle = orbitSpeed * Time.deltaTime;
transform.RotateAround(target.position, Vector3.up, angle);
// オブジェクトBをターゲットから一定の距離に維持
Vector3 direction = transform.position - target.position;
direction.Normalize();
transform.position = target.position + direction * distance;
}
void LookAtTarget()
{
transform.LookAt(target);
}
}
ディスカッション
コメント一覧
まだ、コメントがありません