刚体在当地空间向前移动?

时间:2014-05-20 14:31:12

标签: unity3d

如果你要关闭,我为一个追逐你的敌人制作了一个剧本。如果你走了一定的距离,他会转向你,如果你比这更近,他会追你。令我困惑的是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);
}