- Home /
Finding point of mesh visible by another game object
In my RTS game i have several enemies shooting at the player character.
Right now i use the characters transform.position as the target for the bullet, but what if the character is partly behind some other object? When enemies shoot at him, the bullet ends on the covering object.
I want the enemies to shoot at some point of the character's mesh that is not covered, still visible by the enemy.
Here is an example image of how i want enemies to act:
Hi cattagames,
i would suggest looking at the following link. If you cast a box along your line you will get information for each of the targets hit. It will give you more information with the hitinfo. If you write an if statment that checks if the target hit is an enemy you will get the direction for that specific raycast hit (so the directions of the line from one object to the other). I think this would help a lot, but if you need extra directions please come back to me.
https://docs.unity3d.com/ScriptReference/Physics.BoxCast.html
Thank you for your reply, problem is i already have the line (ray from enemy to player position) but if something else is between the enemy and the player position, the bullet will hit the other object ins$$anonymous$$d. I need to find a way to make that line (ray) to be from enemy position to player's visible point of mesh (if any). To explain it better: if the player is behind a wall but his left arm remains uncovered (seen by enemies), i want enemies to shoot at the left arm.
Yeah thats why i suggest using a boxcast. When you use a boxcast its like a raycast only then bigger. So if you boxcast along the line of your raycast it will see the wall, but also the player and it gives you back the information about where it hits. the player (coordinates of the arm for example) When you use that information, you can then shoot a bullet to the coordinates extracted from the boxcast.
If you dont want to use the boxcast feature i would try doublemax his suggestion.
a) If the model has separate parts for body, head, limbs etc you could check them all.
b) As an alternative, you could cast rays towards the 8 corners of the bounding box. ( -> Renderer.bound.extents )
Yes you could also check a little to the left or a little to the right if the raycast hits a wall.
Answer by Bunny83 · Sep 08, 2016 at 09:09 AM
There is no straight forward solution for this. There are some simple approximation methods (like casting multiple rays to off-center points inside the target object's bounds) however those aren't reliable. This problem can get infinitly complex as there can be multiple objects limiting the view. So it's possible that only a tiny tiny fraction of the target is visible and it's a nightmare to find that with raycasts and analyzing the occluding geometry to find "holes".
A similar problem is solved with VisPortals, but this requires you to setup those areas and visportals manually.
Another way (if a reliable method is required) is to render the scene into a render texture from the attackers point of view using a replacement shader. This way you can render scene geometry black and enemies white. By analysing the pixel data of the render texture you can determine areas where enemies are visible on a per-pixel basis. This of course shouldn't be done every frame since such a check is quite performance heavy.
In the end you should ask yourself if such a system is worth all the hassle. A lot older games had an AI that did exactly what you've shown in your question as "wrong". Other games simply use "A*" or something similar to find a path to the target and simply move along that path until the target is in range and have a direct line of sight.
Thanks for the answer! I knew it would be a difficult thing to achieve, i just asked because that's something i thought other developers may have resolved in an effective way (expecially for FPS games). Since my game is a quite simple RTS-like game and enemies are static, i will leave it as it is and consider it "normal" that enemies are dumb :)
Your answer
Follow this Question
Related Questions
Know if gameObject is being seen 2 Answers
Check if specific camera sees object 0 Answers
raycast cooler 2 Answers
Monster in a certain range of random mobile problems 1 Answer
Making 3D Objects invisible 1 Answer