/// <summary>
        /// Returns the nearest grabbable to a grabPoint position.
        /// </summary>
        /// <param name="grabPoint">position (on controller)</param>
        /// <param name="grabbables">list of objects to grab</param>
        /// <returns>nearest obj to grabPoint in grabbables list</returns>
        public IHandleGrabbing FindClosestGrabbable(Vector3 grabPoint, IEnumerable <IHandleGrabbing> grabbables)
        {
            var             minSqrDist       = float.MaxValue;
            Vector3         realClosestPoint = Vector3.zero;
            IHandleGrabbing closestGrabber   = null;

            // check if someone was grabbed
            foreach (var g in grabbables)
            {
                var closestPoint = g.GetClosestPointToColliderSurface(grabPoint);
                var newSqrDist   = (closestPoint - grabPoint).sqrMagnitude;
                if (g.maxGrabDistance > 0)
                {
                    // if it is close enough for the grab threshold
                    if (newSqrDist < g.maxGrabDistance * g.maxGrabDistance)
                    {
                        if (newSqrDist < minSqrDist)
                        {
                            minSqrDist       = newSqrDist;
                            realClosestPoint = closestPoint;
                            closestGrabber   = g;
                        }
                    }
                }
            }
            //Debug.DrawLine(grabPoint, realClosestPoint, Color.red, 0.1f);

            return(closestGrabber);
        }
        // highlights here. EXTRACT TO NEW SCRIPT IF U WANT REMOTE PLAYERS TO HIGHLIGHT.
        private void UpdateRare()
        {
            // find grabbables somehow. physics sphere cast? register objects and do distance checks? always check all?
            IEnumerable <IHandleGrabbing> grabbables = GrabbableManager.instance.GetGrabbables(transform.position, transform.localScale.x * 10);

            for (int i = 0; i < grabSystem.controllers.Count; i++)
            {
                var c = grabSystem.controllers[i];

                // only do highlight stuff when not grabbing.
                if (!c.isGrabbing)
                {
                    // find closest grabbable. this is expensive, which is why we only do it in rare update.
                    IHandleGrabbing closestGrabber = this.grabSystem.FindClosestGrabbable(c.grabPoint, grabbables);

                    // if found a nearby grabber
                    if (closestGrabber != null)
                    {
                        // if we are not already highlighting the same obj, change.
                        if (!c.isHighlighting || (c.isHighlighting && c.curHighlighted != closestGrabber))
                        {
                            c.Unhighlight();
                            c.Highlight(closestGrabber);
                        }
                    }
                    else // didn't find nearby grabber
                    {
                        if (c.isHighlighting)
                        {
                            c.Unhighlight();
                        }
                    }
                }
            }
        }
 private void GrabController_OnGrabNothing(ControllerGrabberData controller, IHandleGrabbing grabbedObj)
 {
     if (NetServices.isNetworked && NetServices.isClient)
     {
         // spawn!
         // or if networked, ask server for permission to spawn.
         // also give spawn request feedback, and success/fail feedback... but in separate feedback class.
         var msgReq = MessagePool.Get <SpawnGrabRequestMessage>();
         msgReq.isLeft     = controller.isLeft;
         msgReq.prefabType = GetSpawnType(controller.isLeft);
         ClientNetSender.instance.Send(msgReq, UnityEngine.Networking.QosType.ReliableSequenced);
         MessagePool.Return(msgReq);
     }
     else if (!NetServices.isNetworked)
     {
         // spawn! but beware: it might get out of sync with server... so upon reconnect, all those objects must be destroyed or re-synced! :( how the f**k to do that???
         var newObj  = PrefabManager.instance.Spawn(prefabType, controller.grabPointGO.transform.position, controller.grabPointGO.transform.rotation);
         var grabber = newObj.gameObject.GetComponent <IHandleGrabbing>();
         if (grabber != null)
         {
             controller.Grab(grabber);
         }
         Debug.Log("<color=#aa6000>Spawned non-synced object</color>", newObj.gameObject);
     }
 }
        public void Highlight(IHandleGrabbing obj)
        {
            _prevHighlighted = _curHighlighted;
            _curHighlighted  = obj;
            isHighlighting   = true;

            obj.OnHighlight(controller.gameObject);
        }
        public void Grab(IHandleGrabbing closestGrabber)
        {
            this._prevGrabbed = this._curGrabbed;
            this._curGrabbed  = closestGrabber;
            isGrabbing        = true;

            if (isHighlighting)
            {
                Unhighlight();
            }

            closestGrabber.OnGrab(grabPointGO);
        }
Beispiel #6
0
        private void VrGrabController_OnGrab(ControllerGrabberData controller, IHandleGrabbing grabbedObj)
        {
            if (!NetServices.isClient)
            {
                return;
            }

            // deactivate rb sync while grabbed
            var rbsyncer = grabbedObj.gameObject.GetComponent <RigidbodySyncComponent>();

            rbsyncer.StopUpdating(controller.controller);

            // send net message about grab.
            grabMessage.netId    = player.netId;
            grabMessage.leftHand = controller.isLeft;
            grabMessage.syncId   = rbsyncer.syncId;
            ClientNetSender.instance.Send(grabMessage, UnityEngine.Networking.QosType.ReliableSequenced);
        }
Beispiel #7
0
        private void VrGrabController_OnUngrab(ControllerGrabberData controller, IHandleGrabbing grabbedObj)
        {
            if (!NetServices.isClient)
            {
                return;
            }

            // reactivate rb sync on ungrab
            var rbsyncer = grabbedObj.gameObject.GetComponent <RigidbodySyncComponent>();

            rbsyncer.ContinueUpdating(controller.controller);

            // send net message about ungrab.
            throwMessage.netId    = player.netId;
            throwMessage.leftHand = controller.isLeft;
            throwMessage.syncId   = rbsyncer.syncId;
            var rb = rbsyncer.rigidbody;

            throwMessage.position        = rb.position;
            throwMessage.rotation        = rb.rotation;
            throwMessage.velocity        = rb.velocity;
            throwMessage.angularVelocity = rb.angularVelocity;
            ClientNetSender.instance.Send(throwMessage, UnityEngine.Networking.QosType.ReliableSequenced);
        }
        private void Update_LocalPlayer()
        {
            // handle grabbing upon input + send messages when networked.
            for (int i = 0; i < grabSystem.controllers.Count; i++)
            {
                var c = grabSystem.controllers[i];
                if (c.GetPressDownButton())
                {
                    // try to grab now!!!
                    if (!c.isGrabbing)
                    {
                        IHandleGrabbing closestGrabber = grabSystem.FindClosestGrabbable(c.grabPoint, GrabbableManager.instance.GetGrabbables(c.grabPoint));

                        if (closestGrabber != null)
                        {
                            c.Grab(closestGrabber);

                            // NETNETNET (send net grab message)
                            if (OnGrab != null)
                            {
                                OnGrab(c, closestGrabber);
                            }
                        }
                        else
                        {
                            // Trying to grab nonexistent object.
                            if (!c.isHighlighting)
                            {
                                // no highlights either.
                                // here we would be able to spawn+grab the previewed object (or do some other action on the grab button)
                                if (OnGrabNothing != null)
                                {
                                    OnGrabNothing(c, null);
                                }
                            }
                            else
                            {
                                Debug.LogError("[GrabSystem] Highlighting something, yet couldn't grab.... hmmmm *thinks*");
                            }
                        }
                    }
                }
                else if (c.GetPressUpButton())
                {
                    // try to ungrab now!!!
                    if (c.isGrabbing)
                    {
                        c.Ungrab();

                        // this works. apply velocity, angularvelocity here. othwrise it only takes the vel and av from the joint/wobble.
                        //c.prevGrabbed.rigidbody.velocity = V3.up * million

                        // after applying velocity, send message to network about the throw.
                        if (OnUngrab != null)
                        {
                            OnUngrab(c, c.prevGrabbed);
                        }
                    }
                }
            }
        }
Beispiel #9
0
 public void Unregister(IHandleGrabbing grabbable)
 {
     rigidbodyToGrabbable.Remove(grabbable.rigidbody);
     grabbables.Remove(grabbable);
 }
Beispiel #10
0
 public void Register(IHandleGrabbing grabbable)
 {
     grabbables.Add(grabbable);
     rigidbodyToGrabbable.Add(grabbable.rigidbody, grabbable);
 }