fox-handler
fox-handler

Reputation: 131

Rigidbody moving forward in local space?

I made a script for an enemy that chases you if you get to close. If you get a certain distance he'll turn towards you, and if you get closer than that, he'll chase after you. What confuses me a little though is vector.forward or vector (0,0,1). He's supposed to move forward on the Z axis, but since he's always changing rotation to follow you, is it then moving forward in relation to its local space? How does it work exactly?

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);
}                       

Upvotes: 0

Views: 1822

Answers (1)

Related Questions