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

Check if any part of game object is in front of me

Discussion in 'Scripting' started by Strombergr93, Jul 16, 2021.

  1. Strombergr93

    Strombergr93

    Joined:
    Jul 12, 2021
    Posts:
    3
    I'm trying to make some melee attack that will hit in front of me, think of a half-circle. I'm having issues coming up with a way to determine which targets are eligible as accurately as I want. First I'm using an OverlapSphere to determine what's in range, then I'm using code to determine what is in front of me, the problem is that it only checks the center of the objects whereas I want it to be a hit regardless of what part of them is in front of me. Below is what I'm using right now.

    Code (CSharp):
    1.     private bool isTargetInFront(Transform target)
    2.     {
    3.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    4.         Vector3 toTarget = target.position - transform.position;
    5.         float angle = Vector3.Dot(forward, toTarget);
    6.         Debug.Log(angle);
    7.         if (angle > 0f && angle < 3f)
    8.         {
    9.            
    10.             return true;
    11.         }
    12.         return false;      
    13.     }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    OverlapSphere checks if any part of the collider is inside of the sphere, not only the center.
     
  3. Strombergr93

    Strombergr93

    Joined:
    Jul 12, 2021
    Posts:
    3
    Yes, OverlapSphere does that but I need to limit it to the ones in the initial. Perhaps I can use OverlapSphere twice in some way to only check in front of me but how would I ensure that the second check with OverlapSphere is only in front of me?
     
  4. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
  5. Strombergr93

    Strombergr93

    Joined:
    Jul 12, 2021
    Posts:
    3
    Managed to solve it using OverlapSphere then combining it with a second OverlapSphere and then matching the ones that are in both of the spheres to properly apply damage to.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    Understanding how to transform between different object spaces will help with this kind of problem.

    Get the world position of the thing you want to check. Convert it to a local position for the object doing the checking, with transform.InverseTransformPoint(...). Then see if the Z value is positive (in front) or negative (behind).

    Note that to check "any part" of a GameObject you can do stuff like use the extents of its bounding box, or give it some custom nodes to check against, or look at the verts in its mesh (costly!). It doesn't just have to be the GameObject's position.