【发布时间】:2017-11-24 02:10:29
【问题描述】:
这是脚本,我也将它附加到聚光灯上,当玩家在路点之间移动时,聚光灯会旋转并照亮玩家。
但是,当我将脚本也附加到炮塔时,炮塔会旋转 180 度然后停止并且永远不会继续旋转以跟踪玩家。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateSpotlight : MonoBehaviour
{
public GameObject target;
public float smooth = 1f;
public float rangeSqr;
public float rotationSpeed;
Quaternion originalRotation;
private void Start()
{
originalRotation = transform.localRotation;
}
private void Update()
{
if (target.transform.position.x < transform.position.x + rangeSqr)
{
var targetRotation = Quaternion.LookRotation(target.transform.position - transform.position);
var str = Mathf.Min(rotationSpeed * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
}
else
{
var str = Mathf.Min(rotationSpeed * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, originalRotation, str);
}
}
}
【问题讨论】:
-
你的问题到底是什么?
-
@roelofs 为什么炮塔没有面向玩家旋转。玩家在航路点之间移动,但炮塔没有跟踪它。但是,如果附加脚本例如用于聚光灯,则当玩家在范围内时,聚光灯将继续跟踪玩家。
-
那么炮塔代码是什么样子的呢?我认为这信息太少,无法处理。
-
@roelofs 在我的问题中,我添加了脚本,这是附加到炮塔的脚本。对于一个测试,我用球体和立方体创建了一个我自己的炮塔,它旋转得很好。我只是想知道这个炮塔有什么问题。