如果你要关闭,我为一个追逐你的敌人制作了一个剧本。如果你走了一定的距离,他会转向你,如果你比这更近,他会追你。令我困惑的是vector.forward或vector(0,0,1)。他应该在Z轴上向前移动,但由于他总是改变旋转跟随你,它是否会向前移动相对于它的局部空间?它是如何工作的?
var Target : Transform;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
var lookAtDistance = 25.0;
var attackRange = 15.0;
var Distance;
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
renderer.material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
renderer.material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
答案 0 :(得分:1)