I'm creating a tank fighting 2D game top-down in Unity 3D in the 2D perspective active. My axises are X and Y (defaulted) and I'm trying to create a homing missile
My missile code is code is:
var enemyName :String; // which enemy does this shot seeks
private var target :GameObject;
var speed :float = 100;
private var timeToTarget:float;
function Start () {
timeToTarget = Mathf.Abs(speed);
if (timeToTarget<=0)
{
timeToTarget=150;
}
target = GameObject.Find(enemyName);
Destroy(this.gameObject, timeToDie);
myTransform = transform;
}
function Update ()
{
rorate2target();
follow();
}
function follow() //works fine
{
transform.Translate(0,0,speed*Time.deltaTime);
}
and
function rorate2target()
{
/*
var rot : Quaternion = Quaternion.LookRotation( target.position - transform.position, -Vector3.forward );
transform.rotation = rot;
*/
var rot : Quaternion = Quaternion.LookRotation( target.position - myTransform.position, -Vector3.forward );
myTransform.rotation = rot;
}
I tried both options, the 1 in the // and the one without. I tried LookAt that makes it rotate through the Z axis as well as stuff like
Quaternion rotation = Quaternion.LookRotation
(Target.transform.position - transform.position, transform.TransformDirection(Vector3.up));
transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
None helped. What I want the missile to do is to "look" at the direction of its flight.