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 castor · Oct 19, 2012 at 05:54 PM · aifunctionsactions

Creating a "story" for a Character

Extremely new to scripting and don't even know what I need to look for in this question or if formulated correctly.

I have a 2D top down game and there are several NPCs that have their daily routine. What I want to do is find the best way to write their daily routine and how they will deal with the Player character or any other issues.

My ideas was to create several functions that allow to do basic actions (Go to, Open, etc) and write them almost like a movie script. My issue is understanding how it all works and how I can get a sequence of actions and reactions in the best way possible.

This is what I have so far:

 function Update () {
     
     //First action is to move to the Fridge
     GoToObject(objManager.objFridge);
 
     //Once arrived at the Fridge to Open the Door
     if (ArriveAtObject == true){
         OpenObject(objManager.objFridge);
     }
     else{
     }
 
 }
 
 function GoToObject (target : Obj_Properties){
     selectedObject = target;
     GetComponent(NavMeshAgent).destination = target.transform.position;
     isGoingToSelectedObject = true;
     
 
 }
 
 function ArriveAtObject (){
     // Detects if the actor arrives at the SELECTED OBJECT PIVOT
     if ((transform.position - selectedObject.transform.position).sqrMagnitude <= (transform.position * percentageDifferenceAllowed).sqrMagnitude){
         actorAtSelectedObjectPivot = true;
     }
     else {
         actorAtSelectedObjectPivot = false;
     }
 
     //once actor arrives at the destinationObj
     if(isGoingToSelectedObject && actorAtSelectedObjectPivot){
         isGoingToSelectedObject = false;
         actorAtSelectedObjectPivot = false;
         GetComponent(NavMeshAgent).destination = transform.position;
         return true;
         
     }
     else {
         return false;
         
     }
 }
 
 
 function OpenObject (target : Obj_Properties){
     target.objIsOpen = true;
 }

I was hoping I would just write action after action as functions with If statements and have a kind of AI behaviour. But in this example the actor won't even open the Fridge door.

I also attempted to just write this as one big function on the Update but once I add too many actions things start not to work that well and it becomes extremely messy if I want some branching based on player actions.

 function WifeFirstDay (){
 // Calculate a path between the player and the object
     var targetPath : NavMeshPath = NavMeshPath ();                                            //Name of the path calculated
     var possiblePath = GetComponent(NavMeshAgent).CalculatePath(targetPosition, targetPath);// Is a path possible between player and object?
 
 
 
 ///////////            SCRIPT START            ///////////////
     //actor wants to go to the Fridge
     selectedObject = objManager.objFridge;
     GetComponent(NavMeshAgent).destination = selectedObject.transform.position;
     isGoingToSelectedObject = true;
     
     //once actor arrives at the Fridge
     if(isGoingToSelectedObject && actorAtSelectedObjectPivot){
         selectedObject.objIsOpen = true;
         isGoingToSelectedObject = false;
         actorAtSelectedObjectPivot = false;
         
         //Find if there is milk in the fridge:
         for (var child : Transform in selectedObject.transform) {
             if (child.gameObject.name == "Obj_Milk"){
                 var inventoryObject = child.gameObject;
                 inventoryObject.transform.parent = transform;
             }
             else {
             inventoryObject = null;
             }
         }
     }
     
     yield WaitForSeconds (3);
     
     //Result if the actor found milk or not.
     if (inventoryObject.name == "Obj_Milk"){
         selectedObject.objIsOpen = false;
         selectedObject = objManager.objKitchenTable;
         GetComponent(NavMeshAgent).destination = selectedObject.transform.position;
         isGoingToSelectedObject = true;
     }
     else if (inventoryObject == null){
         print("Where is the milk?");
         
         /*
         FindActor("PLAYER");
         if (FindActor){
             //ask where is the milk.
         }
         else {
             //go back to bedroom
         }
         */
     }
Comment
Add comment · Show 5
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 hvilela · Oct 19, 2012 at 10:53 PM 0
Share

What is your question?

avatar image castor · Oct 19, 2012 at 11:33 PM 0
Share

$$anonymous$$y questions is, how would I go about creating a routine for a NPC character? Do I create a set of functions that are lined based on IF comments or do I just write it all inside Update?

I've read some other similar questions in Unity but they seem to be more focused on guard routines or chasing the player and i wanted something more like the AI having a set of tasks he wants to do like the example above (going to the fridge, picking up the milk from it and using it with a bowl to make cereals) and how to react if there is no milk in the fridge or if he can't find the bowl etc.

avatar image hvilela · Oct 19, 2012 at 11:48 PM 0
Share

if you want to create a simple behavior, if's inside the update is enough, if you want to create complex behaviors than the DaveA advice is great, look at state-machines and your life will change. :) In the asset store there's one out of the box for a reasonable price. http://u3d.as/content/dft-games/typed-finite-state-machine/1WS

avatar image castor · Oct 19, 2012 at 11:51 PM 0
Share

thanks! First time i heard "state machine" trying to look into it right now to understand what it means and how it works. Like I mentioned, my background is not program$$anonymous$$g so I'm still trying to understand the terms and tools to create gameplay.

Can I convert DaveA's comment into a answer?

avatar image hvilela · Oct 19, 2012 at 11:53 PM 0
Share

I guess you don't have enough karma for this.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by DaveA · Oct 19, 2012 at 08:09 PM

You should look for 'AI' and 'state machine'

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

11 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

Related Questions

Ai Upgrade?? Possible 1 Answer

Ai Zombie Melee Attack script. 5 Answers

Enemy following Player on uneven surface 1 Answer

ai spot you with flashligt on!!! 1 Answer

AI controller script, Enemy movement issues 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