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 Dominique · Dec 20, 2009 at 07:56 PM · aiproximity

Detect player in range of enemy...

How can I detect if the player is within a certain range of an enemy and only then start tracking the player.

Related question: Efficient spatial searching (finding game objects within a certain range)

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

4 Replies

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

Answer by ericksson · Dec 20, 2009 at 08:48 PM

Jester answer explains things quite well. The only thing his solution doesn't take into account is the case where some obstacle is between the player and the enemy, that obstacle blocking the enemies view towards the player. I add here an example code from that is an implementation of jester's solution plus the idea I suggested above.

//if an enemy as further than maxDistance from you, it cannot see you var maxDistanceSquared = maxDistance * maxDistance; var rayDirection : Vector3 = playerObject.transform.localPosition - transform.localPosition; var enemyDirection : Vector3 = transform.TransformDirection(Vector3.forward); var angleDot = Vector3.Dot(rayDirection, enemyDirection); var playerInFrontOfEnemy = angleDot > 0.0; var playerCloseToEnemy = rayDirection.sqrMagnitude < maxDistanceSquared;

if ( playerInFrontOfEnemy && playerCloseToEnemy) { //by using a Raycast you make sure an enemy does not see you //if there is a bulduing separating you from his view, for example //the enemy only sees you if it has you in open view var hit : RaycastHit; if (Physics.Raycast (transform.position,rayDirection, hit, maxDistance) && hit.collider.gameObject==playerObject) //player object here will be your Player GameObject { //enemy sees you - perform some action } else { //enemy doesn't see you }
}

This code should be added in the Update function of your EnemyAI script. This script will be attached to everyone of your enemies.

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 Dominique · Dec 23, 2009 at 12:51 AM 0
Share

Thanks for the code sample Ericksson, this is just the sort of thing I'm looking for. Thanks to everyone else who answered.

I'll have to see if any of the pathfinding extensions out there are effecient enough to work on the iPhone for my enemies now.

avatar image
2

Answer by jester · Dec 20, 2009 at 08:24 PM

you can subtract the world position of the player from the world position of the enemy to get a distance vector between the player and that enemy. you can then check the length of that distance vector with the "magnitude" member variable of the Vector3 class. if the length of the vector is less than a certain threshold, the player is within range of that enemy.

if you're not concerned with line of sight, that should work fine. you could also check the angle between that distance vector against the current forward vector of the enemy and see if the player could be seen by that enemy or if the player is behind or too far to the side to be seen.

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
0

Answer by Ricardo · Dec 20, 2009 at 11:49 PM

This has been asked before. Look for other questions tagged proximity.

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
0

Answer by ratnadewi1104 · Aug 18, 2018 at 03:04 AM

sorry admin, I want to ask whether in this script range must use AI NavMeshAgent?

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

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

1 Person is following this question.

avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

My Zombie Model is coming at me sideways with the AI 1 Answer

Movement around a huge object by avoiding obstacles 1 Answer

Can See Target not working 1 Answer

Enemy Attraction/Aggro 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