• 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 cattagames · Sep 05, 2016 at 04:17 AM · objectaishootingvisible

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: alt text

immagine.jpg (27.0 kB)
Comment
Add comment · Show 5
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 hoogemast · Sep 05, 2016 at 08:33 AM 0
Share

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

avatar image cattagames hoogemast · Sep 05, 2016 at 06:27 PM 0
Share

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.

avatar image hoogemast cattagames · Sep 08, 2016 at 07:34 AM 0
Share

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.

avatar image doublemax · Sep 05, 2016 at 06:44 PM 0
Share

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 )

avatar image Benjames · Sep 06, 2016 at 03:59 AM 0
Share

Yes you could also check a little to the left or a little to the right if the raycast hits a wall.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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 cattagames · Sep 12, 2016 at 10:05 AM 0
Share

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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does my AI miss me every few feet? 1 Answer

Invoke shoot delay problem. 3 Answers

Fade Object left to right 1 Answer

Enemy Will not die 1 Answer

Code running in a previous state with old data after setting objects to active 1 Answer


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