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. Dismiss Notice

Resolved How to detect the gameobject between 2 points?

Discussion in 'Scripting' started by Epickidman, Sep 13, 2020.

  1. Epickidman

    Epickidman

    Joined:
    Sep 13, 2020
    Posts:
    7
    I want to detect the obstacle between my character and the point I want to path find to. I already know how to find out if there is an obstacle in the path but I want to be able to put that gameobject into a variable.

    This is my script for finding out if there is an obstacle
    int obstacle = LayerMask.GetMask("Obstacle");
    if (Physics.Linecast (transform.position, hit.point, obstacle))

    //This where I want to find out the gameobject I hit
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Use a Raycast instead of linecast. Raycast can provide info about the hit object.
     
  3. Epickidman

    Epickidman

    Joined:
    Sep 13, 2020
    Posts:
    7
    Thanks for the response but I want info about any object hit by the line not only the hit point.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    RaycastAll
     
  5. Epickidman

    Epickidman

    Joined:
    Sep 13, 2020
    Posts:
    7
    Thanks a lot! As you can tell I am not a veteran of unity.