1
votes

i am stuck while coding some multiplayer game using unity 5 UNET. currently am working with unity 5.1 and want to sync some child transform of game object over network.

the problem is that all clients are sending request to sync their child transform to server. but server isn't sinking its child transform with other clients.

i heard that there is a component is unity 5.2 for that work but as i told you i am working in unity 5.1 so i want solution for 5.1.

here is my piece of code.

looking forward for response

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class Sync_Position : NetworkBehaviour 
{
    [SyncVar]
    Vector3 []sync_pos;

    [SerializeField] Transform []obj;
    [SerializeField] int lerp_rate;

    Vector3 []last_pos;
    float threshold=5;

    void Start()
    {
        sync_pos= new Vector3[obj.Length];
        last_pos= new Vector3[obj.Length];
    }

    // Update is called once per frame
    void FixedUpdate () 
    {
        transform_pos();
        lerp_pos();
    }

    void lerp_pos()
    {
        if(!isLocalPlayer)
        {
            for(int i=0;i<obj.Length;i++)
            {
                if(Vector3.Distance(obj[i].localPosition,sync_pos[i]) > 1)
                {
                    obj[i].localPosition=sync_pos[i];
                }
            }
        }
    }

    [Command]
    void Cmd_for_pos(Vector3 p, int index)
    {
        sync_pos[index]=p;
    }

    [ClientCallback]
    void transform_pos()
    {
        if(isLocalPlayer)
        {
            for(int i=0;i<obj.Length;i++)
            {
                if(Vector3.Distance(obj[i].localPosition,last_pos[i]) > threshold)
                {
                    Cmd_for_pos(obj[i].localPosition,i);
                    last_pos[i]=obj[i].localPosition;
                }
            }
        }
    }
}
1

1 Answers

0
votes

You can create "shadow objects" for each connected client/player on the server and then move these objects represening the position of antoehr remote object.

I can only recommend checking out this tutorial since this is what I used myself to get into UNET.

https://www.youtube.com/watch?v=NLnzlwCRjgc