Lock On Projectile

So what I want to do is, create a projectile and fire it at a specific target. We are creating a 3rd Person RPG and spells and such fly at the target. As it is we have the targeting down but I am having problems getting the projectile to fire from "Player" to the "Target". For some reason i just can't think 3D and how to get the math to move from Player to Target.

Any help would be wonderful.

//test attack
if (GUI.Button (Rect (50, 200, 25, 25), "Attack")){
    var instantiatedProjectile : Rigidbody = Instantiate (projectile, PC.transform.position, PC.transform.rotation);
    instantiatedProjectile.velocity = PC.transform.TransformDirection(Vector3(0, 0, 0));
    Physics.IgnoreCollision (instantiatedProjectile.collider, PC.transform.root.collider);

    targobject.GetComponent("Targetable").Life = targlife - 2;
    targobject.GetComponent("Targetable").targeted = true;

You can get the direction to your target by doing this:

Vector3 direction = target.transform.position - player.transform.position;

Then all you need to do is move the projectile in "direction".

Ok so after a bit of looking around and working I came across LookAt, which seemed to solve the issue. I mean the projectile fires straight, but needs to fire at the target when first done. Here's what I did

if (GUI.Button (Rect (50, 200, 50, 25), PC.GetComponent("Attributes").Spell)){
    if (target != "None"){
        PC.GetComponent("Attributes").SpellDamage = 2;
        var instantiatedProjectile : Rigidbody = Instantiate(projectile, PC.transform.position, PC.transform.rotation );
        Physics.IgnoreCollision( instantiatedProjectile.collider,PC.transform.root.collider );
        instantiatedProjectile.velocity = PC.transform.TransformDirection(Vector3(0,0,speed));

Well, @spree gave you the way. But since you didn't get it, allow me to try and guide you through this path.

I hope you're using javascript. And I'm assuming your only issue here is making the projectile move towards the target. Also assuming you'll use the following lines inside some Player's script.

private var targobject : GameObject;
var projectileVelocity : float = 90.0;

function FireProjectile () {
    var instantiatedProjectile : Rigidbody = Instantiate (projectile, PC.transform.position, PC.transform.rotation);
    Physics.IgnoreCollision (instantiatedProjectile.collider, PC.transform.root.collider);
    var targobjectDirection : Vector3 = targobject.transform.position - transform.position;
    instantiatedProjectile.velocity = targobjectDirection * projectileVelocity;

    targobject.GetComponent("Targetable").Life = targlife - 2;
    targobject.GetComponent("Targetable").targeted = true;

targobject must be set somewhere else, inside some script somehow. I've set it as 90 because I guess bullets go at 900 m/s and that would be too fast on Unity.

projectileVelocity is an arbitrary number set on Inspector.

FireProjectile should be called just once per projectile.

It will fire pointed to the target, but it will not follow it. For that, you'd need to write a second script and run it on Update. It could be done on that same script and it would be some more work. Keep in mind that doing an auto-follow with physics, at other hand, will require a lot more of work, and is most likely not needed - there's nothing much physical about such a projectile following a target.

I haven't tested this, so it might need some adjustments, but that's the basic idea.

Or you could just use the UnitySteer functions, particularly steerForPursuit, exemplified on the MpPursuer vehicle and its corresponding PursuerBehavior. Depending on your needs, it's possible that said behavior will require only minimal modification.