Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gilgada · Feb 25, 2013 at 10:50 PM · collisionraycastfieldsight

Help with a field of sight

Im working on getting one of my objects to 'see' another. At the moment I'm just doing a raycast but the problem with this is that it's a single line. This means that unless another object is touching the line, it wont be seen. I need this object to instead have a field of sight branching out from it, similar to how we see. What would be the best way to do this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Feb 25, 2013 at 11:10 PM

Usually this is done by measuring the angle between the target and the look direction: if it's less or equal to the viewing angle, the object is potentially visible - you should then check if a raycast can hit the target without hitting something else in between:

 var viewAngle: float = 30; // degrees from the center
 
 function CanSeeYou(target: Transform): boolean {
   var dir: Vector3 = target.position - transform.position; // find target direction
   var angle: float = Vector3.Angle(transform.forward, dir); // find angle
   var hit: RaycastHit;
   if (angle <= viewAngle && // if inside viewing angle...
     Physics.Linecast(transform.position, target.position, hit) && 
     hit.transform == target){ // and nothing obscuring it...
       // target is visible
   } else {
       // not visible
   }
 }

Call CanSeeYou(target) to check whether it's visible:

 function Update(){
   if (CanSeeYou(enemy)){
     // enemy at sight!
   }
 }

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gilgada · Feb 26, 2013 at 02:30 PM 0
Share

this is fantastic thanks :) i've rewrote it in c# as thats what im using and added in a debug.drawline call to show in the scene when the object is within view. thanks a lot!

avatar image gilgada · Feb 26, 2013 at 02:42 PM 0
Share

one thing I want to add is another condition to the if statement to check whether the distance between the two objects is less than or equal to a known constant. I know that with a physics raycast I could just add this to the end of the parameters such as : Physics.Raycast(transform.position, fwd, out hit, sightRange)

EDIT: I'll try using Vector3.magnitude

EDIT: That's done the trick

avatar image aldonaletto · Feb 26, 2013 at 05:23 PM 0
Share

You could use Raycast ins$$anonymous$$d of Linecast - the direction vector dir was already calculated in the first line of CanSeeYou (in C#, this time):

   ...
   RaycastHit hit;
   if (angle <= viewAngle && // if inside viewing angle...
     Physics.Raycast(transform.position, dir, out hit, sightRange) && 
     hit.transform == target){ // and nothing obscuring it...
       // target is visible
   } else {
       // not visible
   }
 }

Or simply check dir.magnitude, as you probably have done.

avatar image gilgada · Feb 26, 2013 at 06:31 PM 0
Share

Im also now attempting to get the object to turn to face the target as it gets closer. I have the following code:

if (angle != 0) { Quaternion targetRotation = Quaternion.LookRotation(sightDirection); //sightDirection being the result of the targets position $$anonymous$$us the object position

float rotStr = $$anonymous$$athf.$$anonymous$$in(rotationStrength * Time.deltaTime, 1); //rotationStrength is set to 0.5f at the top of the class

transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotStr); }

The problem I'm getting is the object seems to actually look away from the target with this, stopping when the target moves out of the field of sight (because I have this angle != 0 statement embedded into an outer statement that is only true when the target is in view using your help). I think this may be something to do with my model. The forward direction of the model comes out of it's left side. I had to use (1,0,0) to get it to treat the front face as it's forward direction in previous code. How can I change the code above to accomodate the change in vector?

avatar image aldonaletto · Feb 26, 2013 at 10:48 PM 0
Share

The best solution is to child the model to the player and adjust its local rotation so that its front side is in the player forward direction.

Another solution may be to use FromToRotation ins$$anonymous$$d of LookRotation. If its forward direction is left, use this:

 Quaternion targetRotation = Quaternion.FromToRotation(-transform.right, sightDirection);

This works when the model up direction is the world up - if it's different, the model may be rotated to point its Y axis upwards, what will be very weird!

Show more comments
avatar image
2

Answer by nsxdavid · Feb 25, 2013 at 10:59 PM

Get the angle between the objects. Use Vector3.Angle(seeingThing.transform.position,thingToCheck.transform.posion);

Now see if that angle is within the visual fustrum you want. Say +/-45 degrees.

If so then you can do one or more raycasts between the seeing thing and the thing you are checking to make sure there is nothing blocking.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gilgada · Feb 25, 2013 at 11:00 PM 0
Share

Thanks I'll look into that

avatar image
0

Answer by fafase · Feb 26, 2013 at 02:50 PM

As a rule of thumb I always try to avoid recursive functions. Those are square root and all kind of trig functions.

So if Vector3.Angle is right, I would think it uses arccos within. Now using dot you avoid the recursion.

 Vector3 direction = player.position - _transform.position;
 if (Vector3.Dot(direction.normalized, _transform.forward) > 0 )
 {
     // Object is front
 }

0 means 180 degrees field of view, increasing the value will reduce the field, decreasing until -1 will give eagle eye.

All of those computations are simple arithmetic so pretty fast nowadays.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Feb 26, 2013 at 05:32 PM 0
Share

Yes, the dot product of two normalized vectors is the cosine of the angle between them, thus you can compare it to the cosine of the viewing angle.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Collision Detection 4 Answers

Collision Detection If Raycast Source Is inside A Collider? 4 Answers

Optimization of bullet hit calculation per frame 1 Answer

Raycast to OVRplayercontroller,Raycast to OVRplayercontroller - bugged 1 Answer

Raycast Mouse Click On Specific Objects Only 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges