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 /
This question was closed May 07, 2018 at 08:13 AM by Myth for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Myth · Jul 31, 2011 at 04:18 AM · raycastenemyturretdetect

Detection Perimiter

What is the best way to detect an enemy that enters a radius of a turret?

background info.

  • there is no way of telling who the enemy will be, only that they will be an enemy.

  • the turret should have two radius's, one for ground units, the other for air.

  • the air radius will have to be larger.

  • I will need to cast a ray from the gun to the target to check for obstructions.

Any help will be greatly appreciated.

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

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Jul 31, 2011 at 04:35 AM

Use a trigger: child to the turret an invisible object with a capsule collider (uncheck the Mesh Renderer component to make it invisible), adjust it's radius and height to the volume you want to detect and set Is Trigger, then in the OnTriggerStay compare the tag to learn if it's an enemy. If it is, cast a ray to its direction to check if the object is visible:

function OnTriggerStay(col: Collider){
  if (col.tag == "enemy"){
    if (Physics.Raycast(transform.position, col.transform.position-transform.position, detectionRadius){
      // enemy at sight: do whatever you want
    }
  }
}
If the air radius must be much larger than the land radius, repeat the trick: use another trigger to detect it, but ignore the enemy if its position.y is below some height.
Comment
Add comment · 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
1

Answer by bocodeveloper · Jul 31, 2011 at 05:20 AM

and as for the Air Targets and Ground Targets simply have a set range for Air like 100-300 it can hit Air and below 100 it can only target ground which would simply just be the guns second distance check 0-100. I have a some code somewhere if you like to see an example of how to target something at a distance.

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 Myth · Jul 31, 2011 at 06:01 AM 0
Share

It would be nice to be able to see some code that isn't $$anonymous$$e

avatar image
0

Answer by bocodeveloper · Jul 31, 2011 at 05:18 AM

Actually the best way would be to create an array of gameojects and find the closest one to the turret then assign the target to the closest I have a video where I have a turret that fires at closest target: Youtube : http://www.youtube.com/watch?v=X8x8G09u7y4

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 Myth · Jul 31, 2011 at 05:59 AM 0
Share

Tooo much processing for what I am attempting to do.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Two Questions... How to make a tracking turret, how to have enemies home in towards the player's positions 2 Answers

Hit enemy life with raycast 1 Answer

spherecast wont work 2 Answers

Enemy AI's Raycast Ignoring Player 1 Answer

2d raycast enemy detection problem. 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