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 DageLV · Apr 15, 2020 at 02:46 PM · raycastdetectionexplosion

how do i get collision objects?

i did try looking on youtube, every youtuber makes sure to avoid this issue by making their explosion script as useless as it can get.

Basically, i need to check if object is visible or reachable. Explosion happens, i need to know if the explosion will hit the object and if it does, do damage. I have same problem with detecting targets. alt text

untitled2.png (22.8 kB)
untitled.png (26.8 kB)
Comment
Add comment · Show 12
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 Hellium · Apr 15, 2020 at 03:08 PM 0
Share

Isn't a raycast enough for your needs?

avatar image DageLV · Apr 15, 2020 at 04:24 PM 0
Share

@Hellium the images show why raycast isn't enough. The blue represents raycast

avatar image Hellium DageLV · Apr 15, 2020 at 04:28 PM 0
Share

How about a raycast (linecast) on the 4 corners of the boundings box of the tank. You can "optimise" by stopping at the first one returning true.

avatar image DageLV Hellium · Apr 15, 2020 at 06:02 PM 0
Share

whats if wall is blocking the corners, but not the middle?

Show more comments
avatar image Hellium · Apr 15, 2020 at 06:47 PM 0
Share

The following is a pure suggestion, I haven't actually tested it and don't even know if it could be a good idea, if it would be performant or anything.


You can manually draw meshes using Graphics.Draw$$anonymous$$esh. What if you could draw the meshes (tanks, environment, ...) with very simple materials (black color for tank, white for all the other objects) on a temporary render texture with low resolution and then check on the rendered texture whether or not there is a pixel of the same color as the tank's material (i.e a black pixel). If so, your tank is visible from the point of view of the other tank (you will need a camera rendering on the render texture at the players' position)

To gather the meshes to render, you can use a Sphere collider set as Trigger

avatar image streeetwalker Hellium · Apr 15, 2020 at 07:24 PM 0
Share

I had a same idea, which you can see in the reply further down. I don't think you'd need to do black tank. You can set up a camera to render the obstacle to a texture with a transparent background and do the same with tank. Then using the same method as you wrote, you can just go through and find 1 non-transparent pixel in the tank image that has a corresponding transparent pixel in the obstacle image. You know the tank can be seen.

avatar image Hellium streeetwalker · Apr 15, 2020 at 07:34 PM 0
Share

The idea with Graphics.Draw$$anonymous$$esh using plain color materials is to avoid rendering the scene multiple times with complex materials. Using very simple materials will be faster and thus limit the impact on the performances.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Apr 15, 2020 at 05:04 PM

Here is one way to do it: I'm assuming you are working in 2D, [edit] no this won't work in 3D because you have to take Perspective into account so disregard this statemet: but this also works in 3D[/edit]. Rather than multiple Raycasts, do Physics2D.BoxCast the size of the enemy from player to enemy and get any collisions.

Any hits will tell you the enemy is at least partially hidden by an obstacle.

You then need to determine whether or not the overlap of the BoxCast bounds at the position of the obstacle is completely within the bounds of the obstacle - indicating that obstacle hides the enemy completely - or only partially within - indicating that some part of the object can be seen.

Getting bounds of objects and checking them for overlap is fairly simple to do, and I'm not talking about the actual Bounds Class object that is accessible through an object's mesh, though you could possibly employ that. You can use objects scale values combined with the position to create your own bounds.

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 DageLV · Apr 15, 2020 at 06:04 PM 0
Share

im in 3d. i cast the box, okay, i know its being obstructed by a pebble, okay, but how do i know that the other 99.99% of the tank not hiding behind the pebble are visible?

avatar image streeetwalker DageLV · Apr 15, 2020 at 07:11 PM 0
Share

Sorry, I had to rethink. This won't work in 3D, because Perspective is an issue. I amended my answer.

The only options your are left with:

1. doing multiple RayCasts, which will work if objects and obstacles are perfectly rectangular volumes and orthogonal to each other , but as soon as you have odd shaped and objects at angles, it fails.

2. The other option that I will try to detail in my answer is to place a camera at the players location, then render an image of all obstacles and an image of enemy to separate textures, and then digitally use the obstacle texture as a mask over the enemy texture. If there are any pixels left after the masking is done, you know the enemy can be seen.

This method is more complex, and would require setting layers for your enemies and obstacles, and setting up the camera properly, and probably doing a triage by pre-deter$$anonymous$$ing which obstacles are between the enemy and player. But in the end it will be more accurate than ray casts in the event you have odd shaped objects at different angles.

I know all this can be done, but I have to work through the general approach and details.

avatar image DageLV streeetwalker · Apr 16, 2020 at 04:36 AM 0
Share

alt text How do i know the explosion hit only red tank, not the green one? Cause how i understood your idea was if a camera spots white pixel, its hitting all tanks within area, if player sees one pixel of one tank, all tanks in that dirrection are viable targets.

untitled.png (8.0 kB)
avatar image
0

Answer by DageLV · Apr 16, 2020 at 04:44 AM

maybe i can get closest point of all tank colliders to the explosion and shoot raycasts in that dirrection? in most cases that system would work, but there would still be odd angle cases

Comment
Add comment · Show 13 · 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 streeetwalker · Apr 16, 2020 at 07:17 AM 0
Share

I think in this case a Physics.BoxCast will work, even in 3D, because you can aim it at and center of an object, and it will give the hit at point on the box. See the Physics.BoxCast documentation. Box cast

avatar image DageLV streeetwalker · Apr 16, 2020 at 07:28 AM 0
Share

But what if pole is in front of the tank? Won't the box hit the pole and stop?

avatar image streeetwalker DageLV · Apr 16, 2020 at 07:32 AM 0
Share

See the documentation - the box is cast as far as you want it to go, just like all other Casts. It will get the hit on the pole and keep going to get the hit on the tank. Everything hit will be in an array of hit objects.

Show more comments
avatar image streeetwalker · Apr 16, 2020 at 07:40 AM 0
Share

You should watch this series of 2 videos, It may give you some more ideas on how to approach your problems.

https://www.youtube.com/watch?v=rQG9aUWarwE

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

248 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 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 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

How to make a "raycast" vertically in front of the player ? 0 Answers

Raycast hit 2 Answers

Raycast from evemy to player is way to far down 1 Answer

Raycast detect in object (problem) 0 Answers

Raycast to an area, not a point 3 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