私が望むのは、スポットライトをオブジェクトに追従させることだけです。
これは 2 段階のプロセスです。まず、ターゲットの座標位置 (ワールド座標) を見つけます。次に、その位置とオフセットをスポットライトに適用します。あなたのライトは x 軸に沿って 90° 回転しているので、あなたのライトは上にあり、下を見ていると思います。
var offset = new Vector3(0, 5, 0);
function Update()
{
// Move this object
transform.Translate (0, 0, objectSpeed*Time.deltaTime);
// Move the light to transform's position + offset.
// Note that the light's rotation has already been set and does
// not need to be re-set each frame.
lightView.transform.position = transform.position + offset;
}
よりスムーズな「フォロー」アクションが必要な場合は、時間の経過とともに線形補間を行います。交換
lightView.transform.position = transform.position + offset;
と
lightView.transform.position = Vector3.Lerp(lightView.transform.position, transform.position + offset, Time.deltaTime * smoothingFactor);
smoothingFactor
フロートはどこにありますか。
余談ですが、は実際にはコンポーネント検索を行う get プロパティであるtransform.*
ため、あらゆる種類の繰り返しゲーム ループで呼び出すのは死に近いです。GameObject.transform
ほとんどの Unity ドキュメントでは、変換変数を最初にキャッシュすることを推奨しています。
より良いコード:
var myTrans = transform; // Cache the transform
var lightTrans = lightView.transform;
var offset = new Vector3(0, 5, 0);
function Update()
{
// Move this object
myTrans.Translate (0, 0, objectSpeed*Time.deltaTime);
// Move the light to transform's position + offset.
// Note that the light's rotation has already been set and does
// not need to be re-set each frame.
lightTrans.position = myTrans.position + offset;
}