• 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
3
Question by ROM · Jun 19, 2010 at 09:46 AM · iphone

Determining if character is in line of sight?

I'm building a basic 2D stealth game and I want to determine if the guards can see the character. Now the character can hide behind things too so I basically need to determine if the character is in the guards line of sight.

Does anyone know of an easy way to do this?

Thanks in advance!

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

2 Replies

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

Answer by Eric5h5 · Jun 19, 2010 at 08:20 PM

Use Physics.Linecast in combination with Vector3.Angle for things like this. You already know the positions of the guards and your character, so it's easier and faster to use a linecast rather than an array of raycasts. For example:

var fov = 60.0; private var hit : RaycastHit;

function LineOfSight (target : Transform) : boolean { if (Vector3.Angle(target.position - transform.position, transform.forward) <= fov && Physics.Linecast(transform.position, target.position, hit) && hit.collider.transform == target) { return true; } return false; }

The LineOfSight function returns true if the transform that you pass in is within the field of view specified by the public variable, and there's nothing between transform.position and the target transform's position. That's a bit crude since it only takes into account a single line between two transforms, and therefore it wouldn't account for things like someone's head being visible while his body is hidden. So you might want to expand on that depending on the level of realism you're going for.

Comment
Add comment · Show 5 · 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 qJake · Jun 19, 2010 at 09:00 PM 0
Share

How does this work with multiple objects? Does it stop after it hits one thing, or does it collect every object it hit?

avatar image Eric5h5 · Jun 19, 2010 at 09:05 PM 0
Share

It stops after hitting one thing...it's like Raycast, except you pass in two positions ins$$anonymous$$d of a position and a direction.

avatar image ROM · Jun 19, 2010 at 09:14 PM 0
Share

Sorry to intrude but i was just wondering how you would use it in this context. Like how to call it and use it as a variable? I'm rather new to Unity.

avatar image Eric5h5 · Jun 19, 2010 at 09:53 PM 0
Share

Call it like any other function...`if (LineOfSight(target)) {//do stuff}`, given that "target" is a Transform.

avatar image DDaqes · Jan 14, 2019 at 09:09 AM 0
Share

Physics.Linecast function is missing the out keyword before hit.

avatar image
5

Answer by qJake · Jun 19, 2010 at 10:10 AM

Use Raycasting. A raycast is a line drawn out from a point, in a certain direction, which "stops" (and notifies the script) if it's hit something. You can create an array of raycasts in the line-of-sight of each guard, and if any of these raycasts hit the player, then the guard can see him.

If you need to I can draw a picture to help you understand what I'm talking about.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

GetComponentsInChildren not working on iphone 1 Answer

moving an object with accelerometer using unity iphone. 3 Answers

Problem with texture blending and vertex color shader for iOS 1 Answer

Optimizing Projectile Creation and Destruction 2 Answers

Problems Running First App on iPhone 1 Answer


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