Unity2D Multiple Targets with mouseclick (and saving the points)

Hi community,

I got multiple players and I want each of them to move to the points where I clicked with my mouse. But I don’t get it atm, maybe with your help. Here is my script and my inspector.

Controller:

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

public class WagonController : MonoBehaviour
{

    public GameObject target;
    public float speed;
    public Vector3 moveToPosition;
    public Camera mainCamera;
    public List<Vector3> wayPoints;
    private int index;

    // Start is called before the first frame update
    void Start()
    {
        moveToPosition = transform.position;
    }

    // Update is called once per frame
    void Update()
    {

         if (Input.GetKeyDown(KeyCode.A))
         {
             StartCoroutine(Move());
         }

        if(Input.GetMouseButtonDown(0))
        {
            Vector2 raycastposition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast(raycastposition, Vector2.zero);

            if(hit.collider != null)
            {
                if(hit.collider.gameObject.tag == "Player")
                {
                    target = hit.collider.gameObject;
                }
            }
        }

        AddPoint();

        if(target != null)
            {
                target.transform.position = Vector3.MoveTowards(target.transform.position, moveToPosition, speed * Time.deltaTime);
            }
    }

    
     private void AddPoint()
     {
        if(Input.GetMouseButtonDown(0))
        {
            moveToPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            moveToPosition.z = transform.position.z;
            wayPoints.Add(moveToPosition);
        }
     }
     IEnumerator Move()
     {
         while (target.transform.position != wayPoints[index])
         {
             target.transform.position = Vector3.MoveTowards(target.transform.position, wayPoints[index], speed * Time.deltaTime);
             yield return null;
         }
     }
}

And when I select one of my circles, the script switches to the clicked Wagon, and adds a new way point, but when I press “A” my player stops moving.

Here is my problem:
I want to select the player, one of those 3 circles. Then I want to click with my mouse on the map and the clicked player should follow the path I gave him, but not change direction or anything else. He should stop when he is on the last point that I’ve clicked…

I hope it’s understandable…

It sounds like from your response in this thread:

https://discussions.unity.com/t/884405/3

… that you are not willing to actually do the steps that I listed to learn how to do what you want.

The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

This assumes you have at least written and studied some code and have run into some kind of issue.

If you haven’t even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

If you just want someone to do it for you, you need go to one of these places:

https://forum.unity.com/forums/commercial-job-offering.49/

https://forum.unity.com/forums/non-commercial-collaboration.17/

https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

Good luck!

I read your reply about 10 times and I watched more than 50 videos and read more than 100 posts all over the internet, to get a solution for my problem from the last post I’ve written. I even read the Unity Docs about mouseposition more than 10 times and I couldn’t find a single solution, so I decided to change my movement from drawing a line which my player should follow, to a point and click solution. So it’s kind an impudence to say “I’m not willing to learn”… Maybe you should read my question more accurate before replying to peoples posts.

So do you even read my code or looked at my inspector? Because I want help from users that may have had the same problem or know a way to handle my issues.

So next time when you think I’m the kind of girl who want someone to write my code, think about it, read my post instead of judging my own will of learning coding.

It seems like you are good in coding and you are an active member in the unity scene “Posts over 25k”, so maybe you read my post again and give me helpful criticism about my code and not my skill of learning.

Greets,
One

Anyone out there with a constructive solution for my problem?

Hi OneUnity3D, I wonder did you add any 2D box collider to your player gameObject. If there is no 2D collider the “Physics2D.Raycast” will hit nothing when selecting your player.