Getting instantiated homing missile to ignore shooter?

Hi,

I’ve got a homing missile that works great with the following code on it. It gets instantiated in another script and then It finds the closest with game object with the tag “Player” and goes for after it. But how do I make sure it doesn’t go after the player who shoots the missile?

In the full script I use a coroutine to tell the missile to not go after anybody before 2 seconds have passed, but it’s not the best solution.

 private void Start()
{
     rocketRigidBody.velocity = transform.forward * rocketVelocity;
        var rocketTargetRotation = Quaternion.LookRotation(missileTarget.transform.position - transform.position);
        rocketRigidBody.MoveRotation(Quaternion.RotateTowards(transform.rotation, rocketTargetRotation, turn));
}
    public GameObject FindClosestEnemy()
    {
        GameObject[] missileTarget;
        missileTarget = GameObject.FindGameObjectsWithTag("Player");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject GO in missileTarget)
        {
            Vector3 diff = GO.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = GO;
                distance = curDistance;

            }
        }
        return closest;
    }

Probably the most trivial approach: let the missile know who has launched it. When a new target is found, make sure it’s not the same player.

If you don’t want the missile to have that knowledge and this type of “dependency”, one may find various other solutions if you provide more information about the type of missile you want to implement.

1 Like

How do I let i know who fired it? That’s exactly what I need but I don’t know how to do it

The shooter / the script that spawns the missile can set a variable in the missile script that you check against.

Spawner:

newgo = Instantiate(Misselprefab);
newgo.GetComponent<Missile>().origin = this.GamObject;

Missile class

public class Missile : Monobehavior

public GamObject origin;
private void Start()
{
     rocketRigidBody.velocity = transform.forward * rocketVelocity;
        var rocketTargetRotation = Quaternion.LookRotation(missileTarget.transform.position - transform.position);
        rocketRigidBody.MoveRotation(Quaternion.RotateTowards(transform.rotation, rocketTargetRotation, turn));
}
    public GameObject FindClosestEnemy()
    {
        GameObject[] missileTarget;
        missileTarget = GameObject.FindGameObjectsWithTag("Player");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject GO in missileTarget)
        {
            if(GO == origin)
            {
             continue
            }
            Vector3 diff = GO.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = GO;
                distance = curDistance;
            }
        }
        return closest;
    }

there is some other stuff you need to do to get it working but this is the basic idea

2 Likes

I would make it home in on the second closest player

2 Likes

I can get it working from here, but that’s exactly what I need. Thanks a lot for your help.