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.
@Hellium the images show why raycast isn't enough. The blue represents raycast
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.
whats if wall is blocking the corners, but not the middle?
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
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.
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.
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.
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?
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.
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.
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
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.
But what if pole is in front of the tank? Won't the box hit the pole and stop?
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.
You should watch this series of 2 videos, It may give you some more ideas on how to approach your problems.
Your answer
Follow this Question
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