- Home /
missile doesn't move
the missile doesnt move or look at the target, it just sits there and then explodes after 10 seconds heres the script:
var target : Transform;
var explosion : GameObject;
var activated = 1;
function Start () {
Invoke("Destroy",10);
}
function Update () {
if(activated == 1){
transform.Translate(Vector3.forwards * Time.deltaTime * 20);
transform.LookAt(target);
}
if(activated == 1){
if(Vector3.distance(transform.position,target.position)<= 10){
activated = 0;
}
}
}
function OnCollisionEnter () {
Destroy();
}
function Destroy () {
Instantiate(explosion,transform.position,transform.rotation);
Destroy(gameObject);
}
Comment
Best Answer
Answer by aldonaletto · Jul 02, 2012 at 12:50 AM
There's a typo in this line:
transform.Translate(Vector3.forwards * Time.deltaTime * 20);
It should be Vector3.forward, not Vector3.forwards: this error causes an exception that aborts the rest of the routine, thus nothing else happens (excluding the tons of error messages popping in the console view)
You could also declare activated as boolean, what would simplify things:
var target : Transform; var explosion : GameObject; var activated: boolean = true; ... function Update () { if (activated){ // boolean variables are easier to use transform.Translate(Vector3.forward * Time.deltaTime * 20); transform.LookAt(target); if (Vector3.distance(transform.position,target.position)
Answer by juliobds · Jul 02, 2012 at 12:46 AM
Are you getting any warnings or errors? Maybe at: Invoke("Destroy",10);
Besides that I don't see why it shouldn't work.
Edit: Oh, and check if the distance between the missile and the target really isn't smaller than 10