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

help, how to check any collision on the direct line from 2 coord

Discussion in '2D' started by andy30929, Apr 5, 2020.

  1. andy30929

    andy30929

    Joined:
    Feb 26, 2018
    Posts:
    17
    I want to check any collision before doing the next action, but no idea how to implement.

    Consider i have two object in 2D , for an example
    object (4,2)
    object2 (10,16)

    A function to check the direct line between two coordinate (object and object2) without any (object here) collision, return true otherwise return false ? how can i check them ?

    I get a stupid idea,, to create a invisible object as long as the distance by 2 coord, detect any collision with some object then destroy it. any good idea without create one more object ?
     
    Last edited: Apr 5, 2020
  2. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    Use Physics2D.Raycast (https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html) cast ray from object1 in direction of object2 and limit it to magnitude of vector between object1 and object2.

    1) create new Vector2 like this:
    Code (CSharp):
    1. Vector2 toObject2 = object2.transform.position - object1.transform.position;
    2) cast ray:
    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Raycast(object1.transform.position, toObject2.normalized, toObject2.magnitude);
    2.  
    3. if (hit.collider != null) {
    4.     // ... do something
    5. }
     
    MelvMay likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    As stated above, assuming you're using 2D physics colliders, you can use physics queries here. You can do as above (Raycast) or use Linecast (a line segment between two world points) or "cast" one collider or all colliders on a Rigidbody2D through the world to see what it hits (Collider2D.Cast or Rigidbody2D.Cast). Lots of queries giving you lots of options. Worth experimenting with them.
     
  4. andy30929

    andy30929

    Joined:
    Feb 26, 2018
    Posts:
    17
    ummm, the code not work

    Code (CSharp):
    1.     void attack()
    2.     {
    3.         RaycastHit2D hit = check();
    4.  
    5.         if (hit.collider == null)
    6.         {
    7.             shoot();
    8.         }
    9.     }
    10.  
    11.     void shoot()
    12.     {
    13.         Vector2 lookDir = zombie.transform.position - player.transform.position;
    14.         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 270f;
    15.         firepoint.rotation = angle;
    16.         bullet = Instantiate(bulletPrefab, player.transform.position, firepoint.transform.rotation);
    17.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    18.         rb.AddForce(lookDir * bulletforce, ForceMode2D.Impulse);
    19.     }
    20.  
    21.     RaycastHit2D check()
    22.     {
    23.         Vector2 toObject2 = zombie.transform.position - player.transform.position;
    24.         RaycastHit2D hit = Physics2D.Raycast(player.transform.position, toObject2.normalized, toObject2.magnitude);
    25.         return hit;
    26.     }
    when if (hit.collider == null) // always do nothing
    when if (hit.collider != null) // it will always do something even with obstacle

    not work!!!!
     
  5. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    You are probably hitting yourself (player) at the beginning of cast... Print hit.collider.name to see what collider was hit.

    All queries in Physics2D has parameter layerMask to filter what physics layers you want to check during casts. Put your player on different layer from enemies. Then query only for enemies or enemies and obstacles (depends on what your game is about).

    Also as MelvMay mentioned, you can use Linecast, it is simplier as you do not have to calculate direction and magnitude by yourself.
     
  6. andy30929

    andy30929

    Joined:
    Feb 26, 2018
    Posts:
    17
    Physics2D.Linecast(player.position, zombie.position); , and the obstacle is default have collsion

    still not work...........
     
  7. andy30929

    andy30929

    Joined:
    Feb 26, 2018
    Posts:
    17
    Can you show how to dectec any obstacle between within two points, linear cast not working....
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Physics2D.Linecast works fine so honestly, you're either not specifying the positions correctly or there isn't anything that intersects it.

    We can't make it work for you so you'll have to try debugging it. Don't just use your existing code, start a new project add a collider and run a linecast that ovelaps it. I can only presume you've already tried this. You have to start debugging this yourself by seeing what's going wrong in your code.