Search Unity

Unity2D Multiple Targets with mouseclick (and saving the points)

Discussion in '2D' started by OneUnity3D, Jun 13, 2022.

  1. OneUnity3D

    OneUnity3D

    Joined:
    Aug 7, 2019
    Posts:
    28
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WagonController : MonoBehaviour
    6. {
    7.  
    8.     public GameObject target;
    9.     public float speed;
    10.     public Vector3 moveToPosition;
    11.     public Camera mainCamera;
    12.     public List<Vector3> wayPoints;
    13.     private int index;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         moveToPosition = transform.position;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.          if (Input.GetKeyDown(KeyCode.A))
    26.          {
    27.              StartCoroutine(Move());
    28.          }
    29.  
    30.         if(Input.GetMouseButtonDown(0))
    31.         {
    32.             Vector2 raycastposition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    33.             RaycastHit2D hit = Physics2D.Raycast(raycastposition, Vector2.zero);
    34.  
    35.             if(hit.collider != null)
    36.             {
    37.                 if(hit.collider.gameObject.tag == "Player")
    38.                 {
    39.                     target = hit.collider.gameObject;
    40.                 }
    41.             }
    42.         }
    43.  
    44.         AddPoint();
    45.  
    46.         if(target != null)
    47.             {
    48.                 target.transform.position = Vector3.MoveTowards(target.transform.position, moveToPosition, speed * Time.deltaTime);
    49.             }
    50.     }
    51.  
    52.    
    53.      private void AddPoint()
    54.      {
    55.         if(Input.GetMouseButtonDown(0))
    56.         {
    57.             moveToPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    58.             moveToPosition.z = transform.position.z;
    59.             wayPoints.Add(moveToPosition);
    60.         }
    61.      }
    62.      IEnumerator Move()
    63.      {
    64.          while (target.transform.position != wayPoints[index])
    65.          {
    66.              target.transform.position = Vector3.MoveTowards(target.transform.position, wayPoints[index], speed * Time.deltaTime);
    67.              yield return null;
    68.          }
    69.      }
    70. }
    71.  


    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...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    It sounds like from your response in this thread:

    https://forum.unity.com/threads/dra...s-in-2d-without-navmesh.1293672/#post-8198973

    ... 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!
     
  3. OneUnity3D

    OneUnity3D

    Joined:
    Aug 7, 2019
    Posts:
    28
    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
     
  4. OneUnity3D

    OneUnity3D

    Joined:
    Aug 7, 2019
    Posts:
    28
    Anyone out there with a constructive solution for my problem?
     
  5. MaxwellTan

    MaxwellTan

    Unity Technologies

    Joined:
    Mar 3, 2022
    Posts:
    75
    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.