x


Find an object within certain angle in front of current object.

Hey,

Drawing:

              .'
            .'
          .'
        .'   [] //GameObject I want returned.
[   ]--'       //Angle of objects that I want returned.
        '.
          '.
            '.
              '.

So, the first bracket is game object, it scans in front of it at some angle, and returns any gameobjects within that angle, (I'd like the nearest, but I can figure that out). I've been trying to think of how I can get this to work, and I know of methods such as SweepTest, which does roughly what I want, or a simple raycast, but I want it within an angle, not the "sphere" that does 360. I've thought of combining a raycast with a rotating object that'd rotate a certain degree amount and return an array of objects hit (If any) but I don't know how angles work to well, as in, I don't know how to have it literally rotate and while it rotates, it searches and returns (If you can help me with examples, I don't know Eular/Quartarians to well, and I've read the documentation, but their aren't any manually rotate an object that I could find). Finally, my last case I could do, but I don't want to, is spawn the item with 2 empty game objects at the angles I'd like to check, then it simply starts rotated to one, then rotates the other real fast and checks, but this seems a shoddy way of doing it...

My current code that doesn't work (Maybe spark imagination in you? =P):

    var listOfObjects = Physics.OverlapSphere(this.gameObject.transform.position, 50);
    for(var i = 0; i < listOfObjects.length; i++)
    {
       var angle = Vector3.Angle(listOfObjects[i].gameObject.transform.position, gameObject.transform.position);
       if(angle > 1 && angle < 25)
       { 
         return listOfObjects[i].gameObject;
       }
    }
    return null;

It's a 2D thing though, so i don't need anything from other angles.

Any help/links would be appreciated.

more ▼

asked Dec 07 '11 at 11:45 PM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

In your code, Vector3.Angle takes two directions. In other words (7,0,0) represents East, not a spot on the map. The example in the script reference for Vector3.Angle is what you need (find the angle between "target minus me" and "my forwards".)

The angle is always positive (it won't tell you if 10 degrees is clock or counter-clockwise) so can just check if(angle<25)

A problem with the angle method is you miss things very close to you. Imagine a large cow one unit ahead at 45 degrees. A 25-degree angle test misses it, even though its rear legs are right in your face. Widening the angle for close objects seems to help.

For a collider method, maybe you could make a big wedge-shaped mesh and use that as a meshCollider for a triggerBox (not sure meshColliders are allowed for that.)

more ▼

answered Dec 08 '11 at 12:58 AM

Owen Reynolds gravatar image

Owen Reynolds
11.5k 1 7 45

I actually considered doing the wedge-shaped mesh to do this, it just seemed like a very... Backwards?... way of doing it. I did change my code to: var angle = Vector3.Angle(gameObject.transform.position, (listOfObjects[i].gameObject.transform.position - gameObject.transform.position)); as an attempt to do what you said (I believe, target minus me) and it still doesn't work. My output angle has no pattern that I can find. Maybe I'm missing the solution, but do you have any besides the mesh? Thanks!.

Dec 08 '11 at 02:07 AM Justin Warner

I just did the mesh, it doesn't work badly, still seems backwards haha. Thanks =P.

Dec 08 '11 at 03:27 AM Justin Warner

First input to Angle is transform.FORWARD -- the direction you are facing. If you think about it, kind of hard to scan in front of you if you don't tell it where in front of you is.

Thanks for letting me know the mesh works.

Dec 08 '11 at 01:25 PM Owen Reynolds

What you say makes sense, but I can't seem to get it to work. I kinda like the mesh anyways, at least for now... I'll come back to this if I feel a need to find another way. Thanks for your time none-the-less. =)

Dec 08 '11 at 02:15 PM Justin Warner
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x322
x288
x208
x1

asked: Dec 07 '11 at 11:45 PM

Seen: 1270 times

Last Updated: Dec 08 '11 at 02:15 PM