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 rafeekantru · Dec 03, 2013 at 09:50 AM · synchronizationmonster

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;
 
 }    
 

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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

17 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

Related Questions

How to state synchronize the rotation of one axis? 1 Answer

How can I synchronize Two Mecanim Animations? 1 Answer

Does ManualResetEvent work in the Unity runtime? 0 Answers

Check if RPC was received or acknowledged by the client 1 Answer

Unity 5.x Network Synchronization Concepts 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