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 GC1983 · Jan 22, 2013 at 05:06 AM · aidistance

Checking For Player Distance In Front Only

Im setting up a pathfinding enemy AI that will check for distance of the player. That part I have no issue with. What Im unsure about is how to only check in front of the enemy. Anyone know the best way to go about that?

Comment
Add comment · Show 1
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 sdgd · Jan 22, 2013 at 05:10 AM 0
Share

did you try with the mathf rotation ranges for ray?

well don't understand everything and I'd propably need 4 hours to set that but my concept of working toards your problem is this

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Loius · Jan 22, 2013 at 05:19 AM

Check the angle of (playerPosition-AIposition) and (aiTransform.forward).

if ( Vector3.Angle(ai.forward, vectorToPlayer) > sightAngle ) { // 'can't see player'

Comment
Add comment · Show 7 · 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 GC1983 · Jan 22, 2013 at 07:51 AM 0
Share

Vector3 targetDir = target.transform.position - transform.position; Vector3 forward = transform.forward; float angle = Vector3.Angle(targetDir, forward); if (angle < 60.0f)

This game is straight over head, so essentially its 2D.

This is what Im using. It works somewhat...If Im behind the enemy at top side when the enemy is moving downward, they do not see me. But, if Im behind them at bottom side, heading upward, they will see me behind them. If I set the angle > 60, it will be the exact opposite.

avatar image Loius · Jan 22, 2013 at 07:53 AM 0
Share

Vector3.Angle returns the angle in degrees between two vectors.

Your AI sees along "AI.transform.forward" by definition (that's its forward facing, unless you're working with Y+ as 'not up').

The direction from X to Y is always (Y-X), so from your AI to your player is (player.transform.position - AI.transform.position).

If the angle between those vectors is zero, your AI is staring directly at your player. At angle==180, the player is directly behind the AI, and at angle==90, the player is directly beside the AI, but you can't tell which side (without more math).

avatar image GC1983 · Jan 22, 2013 at 08:10 AM 0
Share

so....lol?

avatar image Loius · Jan 22, 2013 at 08:31 AM 0
Share

Add this to your AI's update and it'll draw debug lines indicating the AI's visual range on the X/Z plane, which will help you debug why you're being seen when you shouldn't be:

 var rot : Quaternion = Quaternion.Euler(0,60,0);
 var dir : Vector3 = transform.forward * 20;
 
 Debug.DrawLine(transform.position, transform.position + rot*dir);
 Debug.DrawLine(transform.position, transform.position + (-rot)*dir);

If your guy doesn't rotate then you need to use whatever direction you've stored as its 'facing direction' in place of the .forwards I've been using. I use this myself for simple AI and it works for me.

avatar image Berenger · Jan 22, 2013 at 09:17 AM 1
Share

The angle is more information than you need. Check the sign of the dot product (AI.transform.forward, vector from ai to player). Positive means the player is in front of the AI, negative means he is behind.

If you want your enemy to have a field of view however, you can either use the angle, or normalize the vectors for the dot product and check if it's > something between 0 and 1. Re$$anonymous$$der : angle = acos(A · B)

Show more comments

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

12 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

Related Questions

How would I confine an object to the ground? 2 Answers

Need help with basic AI script. 2 Answers

focusing on a unit during unit detection in an RTS 1 Answer

How to avoid enemies walk up in the air 2 Answers

Following the next item in an array 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