Issues syncing over UNET in Unity3d -
i'm trying sync player's position , rotation on network. i've got things partially working.
i have 2 players host , remote. when looking @ host's screen see correct location local , network player. when on remote see correct location local not networked player.
this sync script:
using unityengine; using unityengine.networking; using system.collections; [requirecomponent(typeof(rigidbody))] [requirecomponent(typeof(networkidentity))] public class syncrigidbody : networkbehaviour { public float positionlerpspeed = 10f; public float positionthreshold = 0.0025f; public float rotationlerpspeed = 10f; public float rotationthreshold = 2f; private rigidbody _rigidbody; private vector3 _requestedpos; private vector3 _lastpos; private quaternion _requstedrot; private quaternion _lastrot; void start () { _rigidbody = gameobject.getcomponent<rigidbody> (); if (!islocalplayer) { _rigidbody.iskinematic = true; } } void update () { transmitposition (); transmitrotation (); lerpposition (); lerpquaternion (); } void lerpposition () { if (!islocalplayer) { _rigidbody.moveposition (_requestedpos); } } void lerpquaternion () { if (!islocalplayer && _requstedrot.w != 0) { _rigidbody.moverotation (_requstedrot); } } [command] void cmdupdatetransformposition (vector3 pos) { debug.log ("cmdupdatetransformposition: " + gameobject.name + " " + pos); _requestedpos = pos; } [command] void cmdupdatetransformrotation (quaternion rot) { debug.log ("cmdupdatetransformrotation: " + gameobject.name + " " + rot); _requstedrot = rot; } [client] void transmitposition () { if (islocalplayer && vector3.distance (_rigidbody.position, _lastpos) > positionthreshold) { debug.log ("transmitposition: " + gameobject.name + " " + _rigidbody.position); cmdupdatetransformposition (_rigidbody.position); _lastpos = _rigidbody.position; } } [client] void transmitrotation () { if (islocalplayer && quaternion.angle (_rigidbody.rotation, _lastrot) > rotationthreshold) { debug.log ("transmitrotation: " + gameobject.name + " " + _rigidbody.rotation); cmdupdatetransformrotation (_rigidbody.rotation); _lastrot = _rigidbody.rotation; } } }
the idea should able toss script on object rigidbody
, should automatically sync on network, local player being source.
why on remote connections i'm not seeing synced object on on host see them correctly?
i not know if here 2 script position , rotation synchronization using works me. maybe not optimised work:
using unityengine; using system.collections; using unityengine.networking; public class player_syncposition : networkbehaviour { private transform mytransform; [serializefield] float lerprate = 15; [syncvar] private vector3 syncpos; private vector3 lastpos; private float threshold = 0.5f; void start () { mytransform = getcomponent<transform> (); } void fixedupdate () { transmitposition (); lerpposition (); } void lerpposition () { if (!islocalplayer) { mytransform.position = vector3.lerp (mytransform.position, syncpos, time.deltatime * lerprate); } } [command] void cmd_providepositiontoserver (vector3 pos) { syncpos = pos; } [clientcallback] void transmitposition () { if (islocalplayer && vector3.distance(mytransform.position, lastpos) > threshold) { cmd_providepositiontoserver (mytransform.position); lastpos = mytransform.position; } } }
rotation:
using unityengine; using system.collections; using unityengine.networking; public class player_syncrotation : networkbehaviour { private transform mytransform; // object rotation vars [syncvar] private quaternion syncobjectrotation; public float rotationlerprate = 15; private quaternion lastrot; private float threshold = 5f; private void start() { mytransform = getcomponent<transform> (); } private void fixedupdate () { lerprotation (); transmitrotation (); } private void lerprotation() { if (!islocalplayer) { mytransform.rotation = quaternion.lerp (mytransform.rotation, syncobjectrotation, time.deltatime * rotationlerprate); } } [command] private void cmd_providerotationtoserver (quaternion objectrotation) { syncobjectrotation = objectrotation; } [clientcallback] private void transmitrotation() { // send rotation server if (islocalplayer && quaternion.angle(mytransform.rotation, lastrot) > threshold) { cmd_providerotationtoserver (mytransform.rotation); lastrot = mytransform.rotation; } } }
Comments
Post a Comment