However I want to create a game environment where NPC’s can travel between scenes, with no input from the players. So if a server is running with no one online then objects on the server still perform actions, for example trading ships traveling between scenes.
I’m guessing the coding logic would be along the lines of the host server handling all this information via a global object, but I’m a little lost on how to achieve this at the moment. What should I be researching to make this possible?
I asked the same question a few days ago, And the basics of it is create an object in your scene and set it to DontDestroyOnLoad, Then that object can use a formula to calculate where the trading ship would be based on speed and time, you could also throw in a 20% chance of pirates etc. You cant have multiple sceens loaded at the same time without taking a performance hit!
When you say “…can travel between scenes…” do you mean actual Unity scenes? If so, you are correct when you assume the host server handles all that. However, I don’t think it is very efficient allowing NPC’s to change scenes. That would require the Application.LoadLevel…blah blah blah which would conflict multiple times if lots of NPC’s are transitioning from scene to scene. Sometimes it is nice to see things in a list, so here’s a list of what I would do:
-Create a movement script for NPC’s that has some waypoints (or pathfinding depending on your game) and goal-oriented objectives for the NPC to meet. This will allow your NPC’s to travel around and finish tasks without you controlling them. Basically: Artificial Intelligence. I use this in my projects for pathfinding: A* Pathfinding Project
-Study up on Networking, specifically Game Servers. Starting with Unity3D-specific functionality. Looking at examples will help you get an idea of how to approach a server that runs things while you are not currently online. The basic idea is that the server is your “world” and players are clients and NPC’s only do things in the world while the server is running. I highly recommend looking at these examples: http://unity3d.com/support/resources/example-projects/networking-example
-Trading ships trading would be handled by making the building/area to trade some sort of “end-goal” based off of a condition. Such as:
void Update()
{
if(shipHasEnoughGold == true)
{
//set waypoint (end goal)
if(isGoalReached)//OnTriggerEnter handles this
{
//make transaction (subtract or add)
//check if NPC still has enough $$$ to trade
//if not, then
//set new waypoint (end goal)
isGoalReached = false; //done with trading
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Trade_Area")
{
//the NPC made it to the goal
isGoalReached = true;
}
}
void Update()
{
// Move the object forward along its z axis 1 unit/second.
transform.Translate(Vector3.forward * Time.deltaTime);
// Move the object upward in world space 1 unit/second.
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
}
The above function calls could be made when the NPC has a new target destination. However, if you use A* Pathfinding, I think there is a end-goal for your character you can set instead of having choppy translates.
Those are some basic examples of things you should try to implement/research to get you started. I hope that helped you get an idea of how to approach this!