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 slayer29179 · May 14, 2014 at 10:47 PM · triggerdistancedetectiontracking

Least heavy processing detection method

Hey all! I have used Unity for nearly 4 years now and I really do love it. Best engine I have used.

Jumping straight to it, what is the best method for detecting items in game?

Example scene: Movable player, NPC on a Nav Nesh.

What's the least processor heavy, most accurate (as in detecting the player at a range AND from any direction), shortest code (without bunching it all together) and / or easiest to manipulate to multiple creatures (as in changing speed, how fast it notices you etc..).

The examples in which I know about all ready are:


1) Shooting Raycasts from the NPC to detect the player.

Advantage: Can control how fast it checks the cast meaning less processing, fairly short code, easy to add to multiple creatures.

Disadvantage: Can only fire in certain directions, I.E forward, back etc... Meaning not very accurate. (Could be fixed focusing on the player only but would ruin anything to do with enemy NPC)

2) Adding a trigger to the character / NPC and checking in OnTriggerEnter and OnTriggerLeave for the other one to enter and leave.

Advantage: Fairly short code, easy to add to multiple creatures, fires in every direction.

Disadvantage: Can't control how fast it checks.

3) Vector distance I.E taking player position from NPC position then using magnitude

Advantage: Easy to add to multiple creatures, fires in all directions and can control how fast it checks.

Disadvantage: Lots and lots of if statements and long code.


Is there any other methods? If not which of the three should be used when many NPC's are spawned?

I don't want anyone to write the exact code. Just the best method so I can code it myself.

Thank you for any assistance!

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
1
Best Answer

Answer by MakeCodeNow · May 15, 2014 at 01:11 AM

As with a lot of questions of this type, the answer is it depends. In particular, it's not clear if you are asking about a many-to-one (i.e. each NPC checking for the player) or a one-to-many (the player checking for all NPCs in range). It also depends on if you need to do the check every frame or if you can afford to check every-so-often.

In the one-to-many case (player checking for NPCs) a trigger is almost certainly the way to go, because internally it's taking advantage of all the sexy and fast spatial partitioning in PhysX. Also, players usually need to do checks like this every frame, which the trigger will do anyway.

In the many-to-one case, a distance squared check is both simplest and fastest. I'm not sure why you said that the distance case requires more checks than any other. As long as you are comparing apples to apples (i.e. from many or from one) then it should be the same number of checks. Usually, I see NPC vision code written like this. You can cut out internal terms if you want to gain speed at the cost of accuracy:

 // Quickly written untested code.
 Vector3 vecToPlayer = player.position - transform.position;
 float squaredDistance = Vector3.SqrMagnitude(vecToPlayer);
 if(squaredDistance < (sightRange * sightRange) // square distance check
 {
     vecToPlayer /= Mathf.Sqrt(squaredsitance); // normalize the vector
     if(Vector3.Dot(vecToPlayer, transform.forward) > sightCone) // view cone check
     {
         if(!Raycast.hit(transform.position, player.position)) // raycast check
         {
             // we saw the player!
         }
     }
 }
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 slayer29179 · May 17, 2014 at 08:09 PM 0
Share

Thank you for explaining both methods! How did you find out that triggers are better or is it just common knowledge?

avatar image MakeCodeNow · May 18, 2014 at 05:18 AM 1
Share

Common knowledge of how spatial queries generally, and physics engines specifically, work.

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

21 People are following this question.

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

Related Questions

Melee for 2.5D Platformer? (C#) 0 Answers

Detect when a trigger has entered a collider 1 Answer

Best Way to Find nearby colliders when you already have a trigger collider on gameObject 1 Answer

Detecting inside an area without a collider 1 Answer

Trigger and raycast error? 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