prefab that has to chase an object

hey dudes!.. yo… i have a problem in doing a prefab follow a gameobject

well… what hapen is… this gameobject cannot be simple draged into a transform var, because i dont know exaclty what object the prefab shall pursuit, it can be a any object in the sceene , the one that is nearest to the prefab will be the one the prefab will chase…

but i dont need help exaclty in the part of discovering witch object is closer… i menage to do this already (with help of cool dudes from this forum) so my problem now is … how to make the prefab GO in direction of the target aquired…

heres my script so far

var mySphere : SphereCollider;  
[COLOR="seagreen"]//this is a sphere collider without colision, i use this sphere to detect who's near[/COLOR]

var targets = gameObject; 
[COLOR="seagreen"]// this var will transform in the closest gameObject that the sphere finds[/COLOR]

var misile : Transform; 
[COLOR="seagreen"]// this is the projectile that i want it to chase the target found by the sphere[/COLOR]



private var wasTriggered : boolean;



function Start(){

    mySphere = GetComponent(SphereCollider);
		
	[COLOR="seagreen"]// start by casting a sphere that goes growing and growing until it touches any object with the tag "aimable"[/COLOR]
	
     

     
  
		
		
}

function Update(){
    if(wasTriggered == false)
        mySphere.radius += 0.5; //tweak to taste
}




function OnTriggerEnter(other : Collider){ 
    if(other.gameObject.tag == "aimable"  wasTriggered == false){
	    mySphere.radius = 0;
        targets = other.gameObject;
        wasTriggered = true;
		
   [COLOR="seagreen"] \\ it touched! ok now that it founded the gameobject, the "targets" var has that object attached to it.. so now i gotta launch the misile prefab (oh, i resized the sphere back to zero too)[/COLOR]


		var LOOKHERE= targets.transform.position;
		
	var sub = Instantiate (misile,GameObject.Find("SUBWEAPONPOINT").transform.position,Quaternion.identity);
     
		misile.transform.LookAt (LOOKHERE) ;

                sub.Rigidbody.AddForce (transform.forward * 900 )

   [COLOR="seagreen"]\\this is the part that doesnt work.. i can cast the misile.. but he is not looking at the LOOKHERE direction.. and when i tell him to go forward.. he goes forward into my point of view, my plan was to make he go forward once he was looking at the target[/COLOR]
		
 
	

		
		
    }
}

actualy, just this part of the code is important

var LOOKHERE= targets.transform.position;
		
	var sub = Instantiate (misile,GameObject.Find("SUBWEAPONPOINT").transform.position,Quaternion.identity);
     
		misile.transform.LookAt (LOOKHERE) ;

                sub.Rigidbody.AddForce (transform.forward * 900 )

  [COLOR="seagreen"] \\this is the part that doesnt work.. i can cast the misile.. 
but he is not looking at the LOOKHERE direction.. and 
when i tell him to go forward.. he goes forward into my 
point of view, my plan was to make he go forward once 
he was looking at the target[/COLOR]

Here:

misile.transform.LookAt (LOOKHERE) ;

You’re modifying the transform of the prefab, not the object that was cloned from the prefab.

Also, you’ll want to add the force in FixedUpdate(), not in OnTriggerEnter(). (The force will likely need to be applied continuously over some number of updates for it to have any noticeable effect.)

okay i undestood that… but how do i modify the transform on the cloned object instead of the prefab? should i code this before i instantiate the misile?

misile.transform.LookAt (LOOKHERE) ;

and… instead of adding force trough this script, i could attach a script on the prefab itself in a fixedupdate to keep adding force right?

You already have a variable that references the cloned object; just use that instead.

Yeah, that sounds right.

oh, you mean this?

var sub = Instantiate (misile,GameObject.Find("SUBWEAPONPOINT").transform.position,Quaternion.identity);

this “sub” var, references the cloned object?

so what i have to do is sub.transform.LookAt (LOOKHERE) ;

right im gonna try it right now! (im at work but i installed unity here too ehehe)

Yes (either directly or indirectly).

Yes (that or something close to that).

it worked perfectly! and that tip about addforce continualy made it look even cooler because now the misile starts slow and then gets more and more impulsion over time…

IT KICKS ASS MAN!

back in home i have a real misile model, here im testing just with cubes LOL… but even so you’re the man! thanks

Hehe…glad you got it working :slight_smile:

too bad there isnt a reputation sistem or i would giving you thumbs up o/