Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mr_Happy_Pixel · Jan 03, 2018 at 05:05 PM · collidercollidersline of sight

collider line of sight

hello , I was wondering if there is a good way to check line of sight between colliders. This could be useful in cases where say an AI npc cant see the center of a player but can see there arm of some other part of the player that sticks out.

scripts in c# please.

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

1 Reply

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

Answer by Lysander · Jan 03, 2018 at 09:52 PM

No scripts in C#, just check the documentation for raycasts. The easiest way to accomplish what you need is to place 'targets' on the extremities of your player, like one on each shoulder, one on each hand, one on the head, one on the chest, etc, and register each of these in a script on the main player object. As long as the enemies have a way of knowing who the players are (some sort of static GameManager where the player objects register themselves when they start, or a cone-shaped "vision range" trigger area where you can store the last-seen players when they walk into it, etc), they should be able to retrieve this list and use it. They can then raycast at these targets each frame, or every X frames, or alternating each raycast in a different frame (latter two for performance reasons). The source of those raycasts would likely just be the face/eyes of the model. The 'targets' are not necessarily colliders (just transform locations), but can be inside of colliders if you want to keep it dead-simple. Raycast against each target, if any player collider is hit, then the enemy can 'see you', but if anything else is hit, they can't.

Alternatively, you can EXCLUDE the player collision layer from the raycast. In that case, the 'targets' on the player don't necessarily need to be inside of colliders (if, for instance, you only have a chest collider because you don't want to be able to shoot peoples' hands, ie: laser tag). The raycast-firing script can just calculate the distance between its own position and the target, fire rays only if that distance is not greater than the max sight range, set the max ray distance as the distance between the enemy and the player target, and only count "hits" if NOTHING is hit by the raycast (ie: no other collider is in the way between those two points). In the situation where the enemy has a reference to the player already stored (which it would have, if you're cycling specific targets this way), then you don't actually need the raycast hit information.

Hope that gives you some ideas. If you need code / demonstrations, just google "Unity raycast example project", or check the project demos on the Unity Learn section of the website. They're incredibly easy to find.

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 Mr_Happy_Pixel · Jan 04, 2018 at 02:58 PM 0
Share

Thanks a ton , I will try this out. Can't believe I didn't think of that.

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

86 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Temporarily ignore collisions between player and enemies 2 Answers

How to check is object directly next to another object? 1 Answer

OnCollisionEnter sensitivity 2 Answers

Multiple Colliders 1 Answer

edited animation colliders, now game is broken 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