Assigning a target to a homing mine

hey you all.
I have a bit of a problem.
Im making a schmup.
And one enemy is a mine.
It has a regular movement.
But when it hits the player with its collider, its should switch from its regular movement and head towards the player. A homing mine.
Ive tried like this thread pointed out: Finding and assigning Target in radius (with colliders) - Questions & Answers - Unity Discussions
But it doesnt give me the player as a target.
And im not sure i understand what they mean in that post.
How do i assign a players transform into an enemies movetowards lock?

Much appreciated for all the help.

this is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MineHoming : MonoBehaviour
{
    public bool agressive = false;
    public Transform target;
    public float calmspeed;
    public float agressivespeed;

    private Rigidbody2D rb;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.velocity = transform.right * calmspeed;
    }

    // Update is called once per frame
    void Update()
    {
        if(agressive==true)
        {
            float step = agressivespeed * Time.deltaTime;
            // Move enemy position a step closer to the target.
            transform.position = Vector3.MoveTowards(transform.position, target.position, step);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag== "Player")
        {
            agressive = true;
            //target.transform.position = collision.transform.position;
        }
    }
}

When the mine collides with the player, just remember the transform of the player. The code you commented out is just setting the targets position to the position of the player:

target.transform.position = collision.transform.position;

What you want is that the mine remembers the transform of the player and can request it’s current position anytime it’s needed:

target = collision.transform;

OMG thank you for answering. I was going out of my mind.
I have commented it out just bcus it wasent working.
And yes, i want it to remember what the target it.
But where should i add the “target = collision.transform”?

Just replace that line you commented out with what i wrote.
This will be the result:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MineHoming : MonoBehaviour
{
    public bool agressive = false;
    public Transform target;
    public float calmspeed;
    public float agressivespeed;
    private Rigidbody2D rb;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.velocity = transform.right * calmspeed;
    }
    // Update is called once per frame
    void Update()
    {
        if(agressive==true)
        {
            float step = agressivespeed * Time.deltaTime;
            // Move enemy position a step closer to the target.
            transform.position = Vector3.MoveTowards(transform.position, target.position, step);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag== "Player")
        {
            agressive = true;
            target = collision.transform;
        }
    }
}
1 Like

thank you so much.
It worked like a charm.
much appreciated.

1 Like

It’s probably worth mentioning that you should check if you actually have a target to follow.
In your Update() method you use target.position, without ever checking it for null.

Whenever you deal with something that can be null (every reference type), you have to make
sure your code will not break if it is ‘null’.
So in your case, it would be good to do the following:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MineHoming : MonoBehaviour
{
   public bool agressive = false;
   public Transform target;
   public float calmspeed;
   public float agressivespeed;
   private Rigidbody2D rb;
   // Start is called before the first frame update
   void Start()
   {
       rb = GetComponent<Rigidbody2D>();
       rb.velocity = transform.right * calmspeed;
   }
   // Update is called once per frame
   void Update()
   {
       if (target == null)
       {
           // There is probably nothing to do for a mine without
            // any target, so we just skip the rest of Update().
           return;
       }

       if(agressive==true)
       {
           float step = agressivespeed * Time.deltaTime;
           // Move enemy position a step closer to the target.
           transform.position = Vector3.MoveTowards(transform.position,
                                                    target.position,
                                                    step);
       }
   }
   private void OnTriggerEnter2D(Collider2D collision)
   {
       if(collision.gameObject.tag== "Player")
       {
           agressive = true;
           target = collision.transform;
       }
   }
}

or something like that, if your mine has other stuff to update.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MineHoming : MonoBehaviour
{
   public bool agressive = false;
   public Transform target;
   public float calmspeed;
   public float agressivespeed;
   private Rigidbody2D rb;
   // Start is called before the first frame update
   void Start()
   {
       rb = GetComponent<Rigidbody2D>();
       rb.velocity = transform.right * calmspeed;
   }
   // Update is called once per frame
   void Update()
   {
       if (target == null)
       {
           // Do whatever needs to be done if the mine does not have
           // a target.
       }
       else
       {
           if(agressive==true)
           {
               float step = agressivespeed * Time.deltaTime;
               // Move enemy position a step closer to the target.
               transform.position = Vector3.MoveTowards(transform.position,
                                                        target.position,
                                                        step);
           }
       }

       
   }
   private void OnTriggerEnter2D(Collider2D collision)
   {
       if(collision.gameObject.tag== "Player")
       {
           agressive = true;
           target = collision.transform;
       }
   }
}