Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by ResistanceGaming · Apr 10, 2017 at 01:52 AM · ainpc

Advice on building an NPC AI

So I am starting work on my first npc ai I would like some advice on what is the best approach to do so.I already have the navigation sorted. I was planing on using trigger boxes (to detect interactions) if statements and boolean's over different 'case' classes all controlled by a main class to essentially build a logic tree to handle situations. is this the best approach? or is there a more efficient way of doing this?

an example of some code:

 publi class npcAiMain : MonoBehaviour {
 
 Public Transform player; //to track players position
 public bool playerInRange; //if true npc can hear or see player
 public bool playerInsight; //if true npc and see player (determined with RAY)
 public bool crimeCommited; //if true npc reports crime if playerInSight true
 
 void Update ()
 {
   player = GameObject.FindWithTag("Player").transform;
 //using FindWithTag as player tag is changed depending on vehicle or not 
  if (playerInRange == true) //check if player is in range
   {
     checkSight(); //check if npc can see player
   }
 
  if (playerInSight == true && crimeCommitted == true)
   {
     reportCrime();
   }
 }
 
 void checkSight();
 {
    Vecto3 pos = transform.position; //determine npc position
    Vector3 target = player.position; // determine player position
 
   RayCastHit hitInfo;
 
 if (Physics.Raycast(pos, target, out hitInfo, 20f)
 {
   if (hitInfo.transform.tag == "Player")
      {
            playerInSight = true;
       }
 else 
     { 
             playerInSight = false;
      }
    }
 }
 void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
             playerInRange == true;
         }
     }
     void OnTriggerExit (Collider other)
     {
         if (other.transform.tag == "Player")
         {
            playerInRange == false;
         }
     }
 
 }


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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Chiroculon · Apr 11, 2017 at 06:41 AM

For what this does so far, this code seems pretty efficient.

This way might be exactly what you need, but that actually depends on your future plans for the NPC AI.

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 ResistanceGaming · Apr 12, 2017 at 06:23 AM 0
Share

$$anonymous$$y future plans for the NPC AI is to make each npc intractable, so I will be using Random.Range function to randomly select between preset personalities, then each one of those will have different quests, conversations or sayings selected in the same way I wanted to have a $$anonymous$$ain script that is essentially just the update function that will check each scenario and then act accordingly and have everything else saved as separate classes attached to the npc so that the main can call on it when needed also to make it easier to edit lines of code. my main concern is whether or not having too many if statements and bools in one function will make it too heavy so to speak

avatar image Chiroculon ResistanceGaming · Apr 12, 2017 at 09:49 AM 1
Share

I scrapped this comment about seven times. Each time I write this comment I get a different idea on what to say. (facepalm) So I'll keep it short:

I think you should consider using finite state machines. (FS$$anonymous$$s)

$$anonymous$$aybe also think about a behavior priority queue.

avatar image
0

Answer by Cuttlas-U · Apr 12, 2017 at 05:22 PM

hi; for detecting u better use raycast; trigger system is not good as some conditions like there is a wall between u and your enemy but trigger goes through it;

i cant tell u all but u can check this tutorial below i promise its very good and optimized ; i used it once before in an android game ; Field of view visualisation

and fore conditions try to use Enum function if u already know how to work with that else search a little there are some tutorials about using enum for creating AI;

oh wait; there is a new good way to creata ai by using sciptable object introduced by unity u can see in here :

AI

sorry cant helo u much with the coding i can just give u an idea on how to do it because its a long way :)

Good Luck

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 sundhar_unity · Oct 27, 2020 at 04:45 AM

This video might Help for NPC Movement : https://youtu.be/BIyYldTyyR8

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

135 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My NPC keeps running through hills and floating 0 Answers

Fixing enemy behavior state machine in Unity 0 Answers

How to make an npc/ai walk next to the player? (C#) 1 Answer

Several NPC with waypoints 1 Answer

Enemy AI script for 2D platformer 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