Networking on Unity 5 (source code)

There is a basic setup of FPSController from Standart Assets, and my Sync Transform Script, you can use it your own project, it’s allow you to Synchronize Position and Rotation of Character, and also the Camera.

You can make your gun as child of the Camera transform and then it will be synchronized over the network. My FPS on the vid is crap (cause my laptop is too op) but it has 0 latency without any recordings.

 

Download here

Start the game with scene called Menu, which is in Scenes folder.

Do not forget to add scenes to the build settings:

Screenshot_5

NetworkSyncTransform.cs, just in case if you need it without downloading the source (put it on the Player prefab):

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

public class NetworkSyncTransform : NetworkBehaviour
{
    [SerializeField]
    private GameObject playerCamera;
    [SerializeField]
    private float _posLerpRate = 15;
    [SerializeField]
    private float _rotLerpRate = 15;
    [SerializeField]
    private float _posThreshold = 0.1f;
    [SerializeField]
    private float _rotThreshold = 1f;

    [SyncVar]
    private Vector3 _lastPosition;
    [SyncVar]
    private Vector3 _lastRotation;
    [SyncVar]
    private Vector3 _lastCamRotation;

    void Update()
    {
        if (isLocalPlayer)
            return;
        InterpolatePosition();
        InterpolateRotation();
        InterpolateCamRotation();
    }

    void FixedUpdate()
    {
        if (!isLocalPlayer)
            return;

        var posChanged = IsPositionChanged();

        if (posChanged)
        {
            CmdSendPosition(transform.position);
            _lastPosition = transform.position;
        }

        var rotChanged = IsRotationChanged();

        if (rotChanged)
        {
            CmdSendRotation(transform.localEulerAngles);
            _lastRotation = transform.localEulerAngles;
        }

        var camrotChanged = IsCamRotationChanged();

        if (camrotChanged)
        {
            CmdSendCamRotation(playerCamera.transform.localEulerAngles);
            _lastCamRotation = playerCamera.transform.localEulerAngles;
        }
    }

    public void InterpolatePosition()
    {
        transform.position = Vector3.Lerp(transform.position, _lastPosition, Time.deltaTime * _posLerpRate);
    }

    private void InterpolateRotation()
    {
        transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(_lastRotation), Time.deltaTime * _rotLerpRate);
    }

    private void InterpolateCamRotation()
    {
        playerCamera.transform.localRotation = Quaternion.Lerp(playerCamera.transform.localRotation, Quaternion.Euler(_lastCamRotation), Time.deltaTime * _rotLerpRate);
    }

    [Command(channel = Channels.DefaultUnreliable)]
    public void CmdSendPosition(Vector3 pos)
    {
        _lastPosition = pos;
    }

    [Command(channel = Channels.DefaultUnreliable)]
    private void CmdSendRotation(Vector3 rot)
    {
        _lastRotation = rot;
    }

    [Command(channel = Channels.DefaultUnreliable)]
    private void CmdSendCamRotation(Vector3 rot)
    {
        _lastCamRotation = rot;
    }

    private bool IsPositionChanged()
    {
        return Vector3.Distance(transform.position, _lastPosition) > _posThreshold;
    }

    private bool IsRotationChanged()
    {
        return Vector3.Distance(transform.localEulerAngles, _lastRotation) > _rotThreshold;
    }

    private bool IsCamRotationChanged()
    {
        return Vector3.Distance(playerCamera.transform.localEulerAngles, _lastCamRotation) > _rotThreshold;
    }

    public override int GetNetworkChannel()
    {
        return Channels.DefaultUnreliable;
    }

    public override float GetNetworkSendInterval()
    {
        return 0.01f;
    }

}
Advertisements

4 thoughts on “Networking on Unity 5 (source code)

      • Ok im back. Everything works smooth for players but I have instanced and spawned objects on the server that are not syncing properly. If I use the network transform, objects cant have rigidbody and cant be kinematic but If I use your script they just dont move at all.

        Like

      • Hi there. Well, the NetworkSyncTransform script won’t work for Non PlayerObjects, just because you need to send commands from client to server. In your case, you could use NetworkTransform component from UNET default components, but don’t forget to switch the mode to Rigidbody (if you want to sync rigidbody) – this way will give you a bit laggy moves, but still. Or you can impelement your own sync script for non player objects, but you need to identify where you want to calculate physics – On Server or on Client. If on the server, you just simply mark your Movement Functions as [Server] attribute and then move your object by changing SyncVar Vector3 of position and then interpolate it on clients as in my script. Hope it will help you, good luck!)

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s