void HandleLinkedRigidbody(OptitrackRigidbody rb)
        {
            Vector3 p = rb.transform.position;

            float yOffset = OptiTrackOSCClient.GetPosition().y;
            float diff    = physicalY - (p.y - yOffset);

            p.y += t.position.y + diff;

            rb.transform.position = p;
        }
Пример #2
0
        // Use this for initialization
        void Start()
        {
            if (instance != null)
            {
                Debug.LogError("There are two OptiTrackOSCClients, remove one");
            }
            instance = this;

            mTransform            = transform;
            server                = new OSCServer(oscReceivePort);
            mTransform.localScale = Vector3.one * scale;

            //playerObject.name = playerObjectName;

            server.SleepMilliseconds = 1;
            //server.Connect();
            server.PacketReceivedEvent += PacketReceived;
        }
Пример #3
0
        void Update()
        {
            if (OptiTrackOSCClient.GetSkeleton(SkeletonAssetName, out skeleton))
            {
                if (!mecanimSetUp)
                {
                    //TODO: Spawn skeleton objects
                    SpawnSkeleton();

                    for (int i = 0; i < subObjects.Length; ++i)
                    {
                        subObjects [i].SetActive(true);
                    }

                    MecanimSetup(SkeletonAssetName, skeleton);
                    mecanimSetUp = true;
                }

                UpdateSkeleton();

                // Perform Mecanim retargeting.
                if (m_srcPoseHandler != null && m_destPoseHandler != null)
                {
                    // Interpret the streamed pose into Mecanim muscle space representation.
                    m_srcPoseHandler.GetHumanPose(ref m_humanPose);

                    // Retarget that muscle space pose to the destination avatar.
                    m_destPoseHandler.SetHumanPose(ref m_humanPose);
                }
            }
            else if (skeleton == null && mecanimSetUp)
            {
                //object has been destroyed... so... reset I guess
                mecanimSetUp = false;
                for (int i = 0; i < subObjects.Length; ++i)
                {
                    subObjects [i].SetActive(false);
                }
            }
        }
        // Update is called once per frame
        protected virtual void Update()
        {
            if (OptiTrackOSCClient.GetRigidbody(rigidbodyName, out def))
            {
                if (def.isActive)
                {
                    Activate();

                    if (t == null)
                    {
                        GameObject g = GameObject.Find(prefab.name);
                        if (g != null && g == prefab)
                        {
                            t = g.transform;
                        }
                        else
                        {
                            g      = (Instantiate(prefab) as GameObject);                       //.transform;
                            g.name = rigidbodyName;
                            t      = g.transform;
                        }
                    }

                    ApplyTransformUpdate(def.position, def.rotation);
                    framesUntracked = 0;
                }
                else if (deactivateWhenUntracked)
                {
                    //prevent untracked objects from lingering in the scene
                    if (++framesUntracked > MAX_FRAMES_UNTRACKED)
                    {
                        Deactivate();
                    }
                }
            }
            else if (deactiveWhenMissing)
            {
                Deactivate();
            }
        }
        protected override void ApplyTransformUpdate(Vector3 position, Quaternion rotation)
        {
            //our gear
            if (name == handShakedName)
            {
                if (verticalWalking)
                {
                    if (rBody == null)
                    {
                        rBody = t.GetComponent <Rigidbody>();
                        if (rBody == null)
                        {
                            rBody            = t.gameObject.AddComponent <Rigidbody>();
                            rBody.useGravity = false;
                        }
                    }

                    //snap to near-ground
                    float yOffset = OptiTrackOSCClient.GetPosition().y;
                    physicalY = position.y - yOffset;

                    Vector3 objectPosition = t.position;

                    //move object to correct X/Z position
                    objectPosition.x = position.x;
                    objectPosition.z = position.z;

                    //raycast down from object
                    RaycastHit hitInfo;
                    //TODO: figure out if we need to raycast from closer to the floor (so we don't teleport on top of stuff too much)
                    if (Physics.Raycast(objectPosition, -Vector3.up, out hitInfo, 2f, 1 << 8))
                    {
                        objectPosition.y += (physicalY - hitInfo.distance);
                        t.position        = objectPosition;
                        rBody.velocity    = Vector3.zero;
                    }
                    else
                    {
                        t.position = objectPosition;
                        if (canFall)
                        {
                            if (withAccelleration)
                            {
                                rBody.AddForce(-Vector3.up * 9.81f, ForceMode.Acceleration);
                            }
                            else
                            {
                                rBody.velocity = -3 * Vector3.up;
                            }
                        }
                    }

                    //send OSC Message for position
                    OSCMessage m = new OSCMessage("/gear-virtualpos");
                    m.Append(name);
                    m.Append(objectPosition.x);
                    m.Append(objectPosition.y);
                    m.Append(objectPosition.z);
                    handshakeClient.Send(m);
                    handshakeClient.Flush();

                    //when previewing in editor, apply rotation (Gear's handle this themselves)
                                        #if UNITY_EDITOR
                    t.rotation = rotation;
                                        #endif
                }
                else
                {
                    //TODO: test
                    t.position = position;
                                        #if UNITY_EDITOR
                    t.rotation = rotation;
                                        #endif
                }
            }
            //other player's gear
            else
            {
                if (verticalWalking)
                {
                    //TODO: test
                    //ignore y, but store it for reference
                    float yOffset = OptiTrackOSCClient.GetPosition().y;
                    physicalY = position.y - yOffset;

                    Vector3 alternateP = t.position;
                    alternateP.x = position.x;
                    alternateP.z = position.z;
                    t.position   = alternateP;
                }
                else
                {
                    //TODO: test
                    //business as usual
                    t.position = position;
                    t.rotation = rotation;
                }
            }
        }