Questions about homing missile-type scripting...

I have a few questions concerning making a script where my Player will shoot out missiles that are supposed to go towards the Player’s Enemy’s current location.


A. If I were to use something like Instantiate() in my Player script to clone a missile prefab, I can’t tell the missile where to go exactly because it has no target (the Player has a target, but how would one tell the missile script that??)

OR

B. If I were to make my missiles a child object of the Player (parent)… I could quickly give the missile script the needed information like Player target and its ‘location’ in transform Vector3 coordinates. However, once I shoot off that missile, if I move my Player at all, wouldn’t the missile also move the same way my player is moving since the missile is a child object? How would I make the missile an INDEPENDENT object AFTER it has the information it needed from the Player via script code??


C. Since I am attempting to make a multi-player game, if I originally did an Instantiate() method, all missiles in the gameplay (from any player) would have the exact missile script attached with the exact same Input.KeyDown() commands. If 5 people are shooting missiles and one player presses a key that causes his own missile to explode, would all the missiles in play explode too or just his own missile because his missile is only controlled by his keyboard? How does that kind of situation work??


I need help on which option (A or B) is better… and an explanation of how C works please…

For A:

When you instantiate the missile prefab, you can set the target of the missile. Which would look something like this:

void Update() {
    if (Input.GetButtonDown("Fire1")) {
        Rigidbody clone;
        clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
        clone.GetComponent<missileScript>.target = player.target; //this is the script on the missile prefab that would control how the missile works..
    }

Of course, since this is mutliplayer, you would need to use Network.Instantiate or whatever the equivalent is of whatever networking code you are using, so everyone knows that the missile was created and who the target is.

B: I wouldn’t make the missile a child object personally, I could see that causing all kinds of problems.

C: The missile shouldnt be the script to have the Input.KeyDown script - that would be on your player. The missile should have its own script attached to it, which controls how the missile acts after it is spawn. Something like:

float missileSpeed = 10;
Rigidbody rb;
public transform target;
Awake(){
rb = GetComponent<RigidBody>();
AligntoTarget();
}

public void Explode()
{
  //explode when this method is called
}

Then you would use an RPC call - to call the Explode method which would explode only the missile you want, and it would explode for all the players in the multiplayer game, not just the local player.

Multiplayer games are hard, good luck my friend.