- Home /
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
}
*/
}
$$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.
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
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?
Your answer
Follow this Question
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