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 Cobradabest · Feb 25, 2014 at 05:09 PM · raycastaiartificial intelligencea.i

How can I make the I see the player better?

I've been making a first person shooter game using Unity. I recently released an alpha of the game to get feedback. One of the most reported problems is that the enemy can't see you unless you're right in front of them. I noticed this myself.

The problem is that they are using a raycast to see the player, and have a very narrow field of view, so I want to know, is there a better way to get the AI to see you without having the game take a big performance hit? I'm going to have a lot of enemies in my games, so performance is important for me.

I was thinking of using trigger colliders to spot the player, would that work, or is that too costly?

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

Answer by dimitroff · Feb 25, 2014 at 06:06 PM

Well, you could use Vector3.sqrMagnitude between the position of the NPC and the Player and set a certain threshold, when the NPC will activate. If you really want to optimize that, you have to separate your map into a grid or use binning.

Comment
Add comment · Show 3 · 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 Cobradabest · Feb 28, 2014 at 12:38 PM 0
Share

Well, okay, how would I do either? Are there any tutorials?

avatar image dimitroff · Mar 01, 2014 at 10:51 AM 0
Share

Well for the Vector3.sqr$$anonymous$$agnitude, you should use this on your enemy control script and you should have a reference to the player:

 Vector3 playerPosition = player.transform.position;
 Vector3 npcPosition = transform.position;
 Vector3 distanceVector = playerPosition - npcPosition;
 
 float sqrDistance = distanceVector.sqr$$anonymous$$agnitude;
 
 if(sqrDistance < SO$$anonymous$$E_DEFINED_RADIUS) {
  // player detected
 }
avatar image Kiloblargh · Mar 01, 2014 at 11:02 AM 0
Share

The distance solution is efficient, but it will let them see through obstacles. So once that triggers you also need to add a Physics.Raycast toward the player's position and see if it hits a wall before it hits the player.

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

ML Agents, combine raycast detection with visual observation? 0 Answers

AI question, characters escaping from light 0 Answers

AI Field of vision 1 Answer

Another Raycast Issue. 0 Answers

Problems with simple AI script 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