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 /
  • Help Room /
avatar image
0
Question by melkamar · Oct 22, 2015 at 09:52 PM · raycastdetection

Raycast to an area, not a point

I am trying to get a AI character detection working. I have a character and a CCTV camera that should detect him.

The camera has a sphere collider on itself and when this triggers, I do a raycast from the camera towards the hit object and see if it is the player. But there is a problem with this approach. The transform.position of the character is at its feet. So if the whole player is visible, except his feet, he will not get detected.

Is there a way to cast from the camera towards an area, not just a single point? That would ensure that whenever any part of the player is visible, it will get detected.

Thanks

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
0

Answer by meat5000 · Oct 23, 2015 at 12:15 AM

Doesn't sound right. Raycasts work against colliders.

Anyway, you can CapsuleCast, SphereCast or even OverlapSphere from the point of your first Raycast hit on the floor.

http://docs.unity3d.com/ScriptReference/Physics.html

Comment
Add comment · Show 4 · 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 melkamar · Oct 23, 2015 at 02:02 PM 0
Share

Raycast do work against colliders. I think we misunderstood each other.

In theory what I am trying to do is Raycast from a point (the camera) towards all the points of the player model and then see if at least one Raycast hit him.

Thinking of it, CapsuleCast might do the trick. Is that a good practice? It would be scaled so that it has a diameter the same as the player's height and should be cast towards the middle of the player model.

avatar image meat5000 ♦ melkamar · Oct 23, 2015 at 02:58 PM 0
Share

Yeah its a perfectly useful method.

avatar image melkamar meat5000 ♦ · Oct 24, 2015 at 04:30 PM 0
Share

Come to think of it, I don't believe it will work. I would create a capsule and then sweep it towards the player. However, if that player was behind something (but visible), the capsule would still hit that something and stop the sweep.

Show more comments
avatar image
0

Answer by Statement · Oct 23, 2015 at 01:56 PM

You could check if the game objects bounding box is within the viewspace or screenspace of that camera.

Here's a class I made for another answer that you may use and modify to suit your needs. For example you may want to use a different camera than Camera.main and you may want to use viewspace instead of screenspace. In particular, you could make a new method based on BoundsToScreenRect that uses WorldToViewportPoint and immediately tests overlap with a new Rect(0, 0, 1, 1)

I updated the class (diff) to include OverlapsViewport methods.

It's possible there's another way to see if the object is within a frustum of a camera. This is just one way to do it. In such case, please do add a comment on how to approach the problem with your idea.

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 Statement · Oct 23, 2015 at 02:30 PM 0
Share

Note: The example code uses renderers bounds and not a collider.

avatar image
0

Answer by springwater · Oct 23, 2015 at 03:38 PM

There is sphere cast.. it may be what you are looking for..or you could get your distance between the cam and player ..instantiate a trigger collision object there and see if you characters in it..

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.

Follow this Question

Answers Answers and Comments

34 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

Related Questions

Raycast does not seem to properly detect object it hit 0 Answers

How best to detect when a charcter object cannot move? 3 Answers

how make raycast detect in object for crosshair 0 Answers

Is there a better way to check if a player is looking at a certain object? 0 Answers

How to make a "raycast" vertically in front of the player ? 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