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 /
This question was closed Jun 13, 2013 at 05:23 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Dr_GeoFry · Mar 19, 2013 at 07:33 PM · animationsdeathfollow playerartificial intelligence

AI Awareness

Hi everybody,

I have code written that makes the enemy follow me based on my proximity to them. What I would like to do to enhance this, is make it so that the enemy would not respond to my presence if I am standing behind him. This would allow for more specialized animations and head shots etc.

I'm just curious what code allows for the AI to have no awareness of things behind the front of their face/back of the head.

Thank you

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Mar 21, 2013 at 10:59 PM

It's easy enough to test the angle between the player and the enemies facing using the "Dot Product" which is the cosine of an angle and is helpfully a number between +1 and -1.

So:

     var dot = Vector3.Dot((player.transform.position - enemy.transform.position).normalized, enemy.transform.forward);

  • dot > 0, player is in front of the enemy

  • dot = 0, at right angles

  • dot < 0, player is behind the enemy

Obviously you can use other values (0.5 is 45 degrees etc).

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 Dr_GeoFry · Mar 21, 2013 at 11:05 PM 0
Share

thank you so much for helping

avatar image
1

Answer by Loius · Mar 21, 2013 at 10:58 PM

 distanceToPlayer = player.position - transform.position;
 
 angleToPlayer = Vector3.Angle(distanceToPlayer, transform.forward);
 
 // 0 <= angleToPlayer <= 180
 // angleToPlayer == 180 -> player directly behind
 // angleToPlayer == 0 -> player directly in front
 // angleToPlayer == 90 -> player either directly left or directly right (or directly above or below)

 if ( angleToPlayer < myPeripheralVisionAngle ) DetectedPlayer();
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 whydoidoit · Mar 21, 2013 at 11:05 PM 0
Share

@Louis - ah @Dr_GeoFry was waiting for an answer for 2 days and then 2 came along at once :)

Answers on UA are like buses...

avatar image Dr_GeoFry · Mar 21, 2013 at 11:12 PM 0
Share

@whydoidoit thanks guys. Actually the situation I was looking for help in is how do I make the enemy unaware of me if I approach him from behind. Currently, the enemy becomes aware of me when I come within a certain distance of him. I want him to be completely unaware of me if I approach from behind.

avatar image whydoidoit · Mar 21, 2013 at 11:15 PM 0
Share

Right so you can use either Louis's answer or $$anonymous$$e. In Louis's you need to have the enemy be unaware when the angle is > 90 (or whatever) and in $$anonymous$$e when the dot value is < 0 (or whatever) - just add that to your distance test.

avatar image Dr_GeoFry · Mar 21, 2013 at 11:17 PM 0
Share

ok, I'm sure that will work. Let me test it out.

avatar image Dr_GeoFry · Mar 21, 2013 at 11:28 PM 0
Share

@whydoidoit I added this code:

 var dot = Vector3.Dot((target.transform.position - enemy.transform.position).normalized, enemy.transform.forward);

Ins$$anonymous$$d of player, I substituted target, because the player is the target. This is a part of the enemy AI script. I get this error when I want to test:

An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.

Show more comments

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Crowd AI Simulation Advice/Recommendations 0 Answers

Script to detect a death fall 2 Answers

I want my 2D character to fall down as chain after he dies 1 Answer

Death on impact with water 1 Answer

Character disappears when dies. 3 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