Make Arrow(Bullet) to follow the player

I want write a Tower script which can shoot arrow to people,tower dosent need to be rotation,just shoot the arrow to people.

but my arrow dosent follow my player,
could anyone help me :frowning:

thanks!


var LookAtTarget : Transform;  
var timeDelay = 5;  
var ArrowPoint : GameObject;  
var Arrow : GameObject;  
 
function Start() {  
 
    while (true) {  
 
        yield WaitForSeconds(timeDelay);
        //The function(The gameObject, THE SPAWNS LOCATION, the spawns direction)  
 
       var arrowClone : GameObject = Instantiate( Arrow, ArrowPoint.transform.position, Quaternion.identity );  
       arrowClone.transform.LookAt(LookAtTarget);
       arrowClone.rigidbody.AddForce(transform.forward * 1000);  
       Destroy ( arrowClone, 15 );
    }  
 
}  
 
function Update () {  
 
}

Read this page : http://answers.unity3d.com/page/newuser.html For any help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window. Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

Oops,Im sorry about that and thanks for your remind. I already format my code now(:

2 Answers

2

Instantiate your arrow into a variable :

var arrowClone : GameObject = Instantiate( Arrow, ArrowPoint.transform.position, Quaternion.identity );

Using that stored reference to the Instantiated arrow, now you can tell it to look at the target :

arrowClone.transform.LookAt(LookAtTarget);

now you have a reference to the instantiated arrow, you don’t have to find it :

Destroy ( arrowClone, 10 );

Cool,thanks a lots!!! The arrow is working now !!!

you could try a Vector3.Lerp(Target.x,Target.y,Target.z);

Oops, is your mean change transform.LookAt(LookAtTarget); to transform.LookAt(Vector3.Lerp(Target.x,Target.y,Target.z)); ? It have error say 'x' is not a member of 'UnityEngine.Transform'. Sorry Im the Unity beginner, can't really understand :( Could you explain to me more?

This answer is incorrect.