- Home /
Problem in adding monster in unity using uLink
strong texti am making an MMORPG using uLink in unity.i am building my setupon the snowbox.my problem is that when i am adding a monster as network player using network.Instantiate it wont work correctly. i can see the monster on client side and server side.But when i apply an AI script to the monster the complete synchronisation is lost.i am attaching the server code and AI script.please anyone give me a solution.
/////GameServer.cs
using System; using UnityEngine; using uLink;
public class GameServer : uLink.MonoBehaviour { public string gameName = "Dragneel";
public int port = 7100;
public int maxConnections = 64;
public string SpawnTag = "";
public string EnemySpawnTag = "";
public GameObject proxyPrefab = null;
public GameObject ownerPrefab = null;
public GameObject creatorPrefab = null;
/********** For the Enemy ***********************/
public GameObject Enemyproxy = null;
public GameObject Enemyowner = null;
public GameObject Enemycreator = null;
public bool isCellServer = false; //true only when part of a "uLink Unlimited" cluster environemnt
public string pikkoServerIP = "127.0.0.1"; //Only used when part of a "uLink Unlimited" cluster environemnt
public int pikkoServerPort = 4001; //Only used when part of a "uLink Unlimited" cluster environemnt
public ChatServer chat;
private string level;
void Awake()
{
level = Application.loadedLevelName.Replace("Server", "");
uLink.Network.isAuthoritativeServer = true;
if (isCellServer)
{
uLink.Network.InitializeCellServer(maxConnections, pikkoServerIP, pikkoServerPort);
}
else
{
uLink.Network.InitializeServer(maxConnections, port);
}
}
void uLink_OnServerInitialized()
{
Debug.Log("Server successfully started on port " + uLink.Network.listenPort);
uLink.MasterServer.dedicatedServer = true;
uLink.MasterServer.RegisterHost("Snowbox", gameName, "", "", level);
/*****************For the Enemy to be Born*********/
string EnemyName = "Villan";
Color EnemyColor = Color.red;
GameObject Enemyspawns =GameObject.FindGameObjectWithTag(EnemySpawnTag);
Transform Enemyspawn = Enemyspawns.transform;
uLink.Network.Instantiate(uLink.NetworkPlayer.server, Enemyproxy, Enemyowner, Enemycreator, Enemyspawn.position, Enemyspawn.rotation, 0, EnemyColor, EnemyName);
}
void uLink_OnPlayerApproval(uLink.NetworkPlayerApproval approval)
{
approval.Approve(level);
}
void uLink_OnPlayerConnected(uLink.NetworkPlayer player)
{
string playerName = "Nameless";
if (player.loginData != null) player.loginData.TryRead(out playerName);
if (uLink.NetworkView.FindByOwner(player).Length > 0)
{
return;
}
Color playerColor = HSVToRGB(UnityEngine.Random.Range(0.0f, 360.0f), 0.3f, 1);
GameObject[] spawns = GameObject.FindGameObjectsWithTag(SpawnTag);
int spawnindex = UnityEngine.Random.Range(0, spawns.Length - 1);
Transform spawn = spawns[spawnindex].transform;
uLink.Network.Instantiate(player, proxyPrefab, ownerPrefab, creatorPrefab, spawn.position, spawn.rotation, 0, playerColor, playerName);
chat.Chat(playerName + " has joined", playerColor);
}
void uLink_OnPlayerDisconnected(uLink.NetworkPlayer player)
{
PlayerCreator playerCreator = player.localData as PlayerCreator;
if (playerCreator != null) chat.Chat(playerCreator.playerName + " has quit", playerCreator.playerColor);
uLink.Network.DestroyPlayerObjects(player);
uLink.Network.RemoveRPCs(player);
}
void uLink_OnHandoverTimeout(uLink.NetworkPlayer player)
{
//This code is needed to destroy the player object (Player@Creator) in the new game server after the handover.
//This code is executed if the client fails to connect to the new game server (for any reason).
uLink.Network.DestroyPlayerObjects(player);
uLink.Network.RemoveRPCs(player);
}
private static Color HSVToRGB(float h, float s, float v)
{
if (s == 0)
{
return new Color(v, v, v);
}
h /= 60;
int i = Convert.ToInt32(Mathf.Floor(h));
float f = h - i;
float p = v * (1 - s);
float q = v * (1 - s * f);
float t = v * (1 - s * (1 - f));
switch (i)
{
case 0: return new Color(v, t, p);
case 1: return new Color(q, v, p);
case 2: return new Color(p, v, t);
case 3: return new Color(p, q, v);
case 4: return new Color(t, p, v);
default: return new Color(v, p, q);
}
}
}
////////////////////////////////////////////////////////////////////////////
AI Script
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class EnemyAI : MonoBehaviour { public float rotaionSpeed; public float moveSpeed; public Transform target;
private Transform myTransform;
private float maxDistance = 10.0f;
public bool villan = false;
void Start()
{
// *** Initializing variables ***//
rotaionSpeed = 5.0f;
moveSpeed = 18.0f;
// *****************//
// target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
//myTransform = transform;
}
RaycastHit hit;
void Update()
{
if(target == null)
{
villan = true;
}
else
{
villan = false;
}
if ( villan == true)
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
myTransform = transform;
}
if(Physics.Linecast(target.position,myTransform.position,out hit))
{
Debug.Log(hit.collider.name);
Debug.DrawLine(target.position,myTransform.position,Color.yellow);
}
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position),rotaionSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance)
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
Answer by Evilcool · Feb 25, 2014 at 03:33 PM
I'm not expert But I know few things that I'll share :
In order to synchronize objects over network you need to :
Use allocated Network Id on prefabs network component that you attached to them prefabs.
Also use OnSerializeNetworkView (ulink version) ! OR ! RPC(s) ! OR Both ! to sync I see none of these and correct me if I'm wrong
These two options are used to share informations between networkUsers how can your client know that your AI has moved from its old position if you don't tell them.
Hope this helps good luck.