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 fighder · Jun 21, 2015 at 04:46 PM · 2dcollisionlightstealth

Collision with light

Trying to create a 2d stealth game where the enemy uses light to show their view range and if the player touches the light, they are detected.

Normally I would use a collider that will follow the enemy around as a mean to create effective range, but the problem this time is since it is light, the view range changes when the enemy is looking at an obstacle, say a wall. The wall blocks off the light, and so if the player is behind a wall, the enemy should not be able to see them, there fore the collider needs to be shorten to match the size of the light.

I was thinking about using raycasting of three directions (above, below, and forward) to check if the ray hits the player first. I feel like this way the player can slip through the rays even if they are in range of the light sometimes.

Is there a good way to do this or is it a hassle no matter what?

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
1
Best Answer

Answer by fafase · Jun 21, 2015 at 05:01 PM

Since you are in 2D, you should use a triangle of linecast. One goes from light position and upwards , one from light position and downwards and one joining the end of both.

See on the drawing below, the two line cast leaving from the light and the one perpendicular.

This way, there is little to no chance a character can be inside without hitting one of the cast first.

Bets is that you place two game objects in front of the player that you parent so they move with him, and you use those to start/end the line cast.

alt text


presentation1.jpg (27.3 kB)
Comment
Add comment · Show 3 · 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 fighder · Jun 21, 2015 at 05:26 PM 0
Share

alt text

Really clever, but I have a few problems about this solution.

In figure one, E (enemy) have the view of the yellow triangle, but the light is only the green part. P (player) is inside the triangle, so even though he is not inside the light, the enemy still detected him.

Of course, from the up and down ray, I know the player is not the first collider hit and so the player can be behind a wall.

But if the same solution applies, there will be another problem in figure two where the up and down ray is the one hitting walls while the third ray is not.

untitled.png (16.8 kB)
avatar image fafase · Jun 21, 2015 at 07:13 PM 0
Share

Quick way I can think of, if one of the linecast hits the player draw a linecast between the enemey and the player position. If that one succeeds then your player is within light and the enemy sees him.

This extra check is only if one of the linecast succeeds.

avatar image fighder · Jun 21, 2015 at 07:15 PM 0
Share

ummm I think that will work, thanks :D

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

21 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

Related Questions

Unity2D side collision detection 1 Answer

How to change the color of the ball after hitting square? 5 Answers

Collision on specific Frames 1 Answer

Why does my OnCollisionEnter2D not work? 3 Answers

Unity2D Collision is bugged 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