Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Object Movement that can stop on collision detection?

Discussion in '2D' started by Interator, Jun 9, 2018.

  1. Interator

    Interator

    Joined:
    Nov 15, 2017
    Posts:
    2
    I'm trying to code troops for my game that could move towards the position of my mouseclick. I got this done with the MoveTowards function, and it works perfectly with one unit. However, when multiple units want to move towards the same position, even with colliders, they start overlapping. I believe that it's because the MoveTowards never finishes, so it keeps trying to move towards the same point, leading to the units clipping over each other. Is there any way to fix this?

    I tried to find a way to cancel the MoveTowards, but that doesn't appear to be possible. I also tried to override it with another MoveTowards function towards the same point on collision detection, but that also doesn't work. I'm searching for other functions that can move to a certain point, but the only other one appears to be lerp, and I can't find any way to stop that one as well. Is there any way to stop this?
     
  2. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    If you look for basic solution, then you can create some bool , that will be turned to off when you will get your target (you can check it by OnCollisionEnter, OnTriggerEnter, transform.position and etc.) And in Update method or where you call MoveTowards method, you can check if this bool still true, if not, then stop calling MoveTowards. Basic example:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class delete : MonoBehaviour
    6. {
    7.  
    8.     public Transform target;
    9.     public float speed;
    10.     private bool keepMovingTowardsTarget;
    11.  
    12.  
    13.     private void Start()
    14.     {
    15.         keepMovingTowardsTarget = true;
    16.     }
    17.     void Update()
    18.     {
    19.         if (keepMovingTowardsTarget)
    20.         {
    21.             float step = speed * Time.deltaTime;
    22.             transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    23.         }
    24.     }
    25.  
    26.     private void OnCollisionEnter(Collision collision)
    27.     {
    28.         if(collision.transform.tag == target.tag)
    29.         {
    30.             keepMovingTowardsTarget = false;
    31.             Debug.Log("We reached our target! I am stopping MoveTowards.");
    32.         }
    33.     }
    34. }
     
    Interator likes this.
  3. Interator

    Interator

    Joined:
    Nov 15, 2017
    Posts:
    2

    Thank you for the response! I'm a bit confused about the code that gets executed at the collision though. When we check the tags, what exactly does that mean?
     
  4. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    There are tons of tutorials about this. Just google it. In two words, you are saying unity that - when i am collision with something, check his tag, if his tag similar to tag of my target, then i am near of my target, so lets just stop here.

    Learn about colliders,triggers:




    Also you can if you have Rigidbody you can make its velocity = vector2d.zero. This is so basic info, that you can find in top 5 searches of google: "how to stop 2d object unity" .