Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by efergan · Jul 15, 2012 at 05:22 PM · enemydetection

Regarding the efficiency of enemy detection

I've got a far bit from reading various questions & answers, but there's a few questions still going through my head. I hope it's alright to air them here.

So I'm trying to do a pretty standard Enemy detection script for an AI state. But as I'm new to Unity (and 3D) I'm unsure the most effcient way.

I'm thinking it should be something like:

  • Check whether the player is within range of the enemy

  • Check to see whether the enemy is rotated towards the player

  • Do a raycast to see whether they have line of sight.

My questions are: What's the most efficient way of checking range? Calculating the distance or checking interaction with a Trigger object? I'm currently assuming that a RayCast would be the most inefficient calculation out of the above steps, so left it until last, do people agree?

Thanks all.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Avaista · Jul 15, 2012 at 06:07 PM

As always, it is very dependent on your desired game behavior.

Trigger Colliders offer a great deal of flexibility most of the time, because you interact with them using OnTriggerEnter or OnTriggerExit, which means different enemies can have diffently shaped agro area(Boxes, Spheres, Capsules and even Mesh triggers(more on this soon)

RayCasts are not the most expensive thing in the world. What is important to remeber when doing any kind of Physics detection (raycasts, triggers) is the Physics Layer matrix (Edit->Project Settings->Physics).

If you cast a ray against only the player layer, for instance, you will only have a few things to detect against, which makes the calculation that much faster. Same concept applies with a trigger collider.

One thing that I have done in the past for automated turrets is give them a cone mesh collider turned into a trigger. If the turret is an Enemy Turret, the collider is placed on the "EnemyTargeting" layer(for this example). Which only interacts(set through the Physics Manager) with things on the "Player" layer. Now I can assume that ANYTHING that enters activates that OnTriggerEnter is something that I want to shoot. After that I send a raycast every .2 seconds towards the player to see if there is anything in the way. Now you can replace raycast with sphere cast or capsule cast or whatever, and you can set the layermask before you fire it, aka

int layerMask = 1 << LayerMask.NameToLayer("Obstacles");

By A) Reducing the number of items I'm checking for by setting a particular layer and B) Reducing the number of Rays Im sending by only shooting one every .2 seconds(rather than every frame [[5 times a second vs 60]]

I can create an efficient, flexible aggro detection for a turret. If the player leavs my trigger, I simply stop shooting Raycasts(you can do this several ways depending on how youve set it up(aka: Coroutines/invoke repeating etc)

Now a slight modification to this is to assume that the trigger is not your viewing area, but rather an area you(in this case your enemy) are defending. Since the trigger no longer represents your target area, you will need to add checks to make sure the target is in.

In the case of a cone, this can be a simple dot product against the enemy forward and the relative location of the player.

IF In trigger AND In cone of vision AND In sight(Raycast point above) THEN RageOfTheBrooklynVariety(target);

Semi-Not-Really-More-Like-Hemi Pro Tip:

Square Root Sucks. If you are doing your ranged detection

(Vec1-Vec2).sqrMagnitude < sqrRange

Is far faster than

(Vec1-Vec2).magnitude < range

Which is what Vector3.Distance is

because distance is x^2 + y^2 + z^2 = d^2

Comment
Add comment · Show 2 · 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 Avaista · Jul 15, 2012 at 06:58 PM 0
Share

I would argue it matters in the long run. Practically no, it will probably not kill you, but it is a good habit to get into.

I work in XNA as well as Unity. One of my favorite stories was about a peer who was making a simple 2D game. He had lag, i mean like 2-3 frames a second lag. We found one place where he had been using the Pythagorean method, with sqare root. Against x enemies every frame. We changed it out and it jumped to 60 frames a second. To be fair, there were alot of enemies. But the effect was hilarious.

avatar image efergan · Jul 15, 2012 at 09:21 PM 0
Share

Thanks guys. Appreciate the full and quick responses. Like the idea of the targeting 'cone', solves a lot of problems all pretty neatly.

We use Pythagorean equations all the time (2D games too), good to know if things start grinding to a holt.

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

6 People are following this question.

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

Related Questions

Enemy detection while pathfinding through the map 1 Answer

Player attacks once but the enemy takes double damages! 1 Answer

How to correctly do an enemy ranged attack script? 0 Answers

Enemy Detection Area 1 Answer

How to detect enemy in effective way? 2 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