private void UpdateGrasp()
        {
            int mouseButton = 2;
            var mainCamera  = FindCamera();

            if (Input.GetMouseButtonDown(mouseButton) && grasped != null)
            {
                grasped.Release(hand);
                grasped = null;
                transform.localPosition = startingPosition;
            }

            if (Input.GetMouseButtonDown(mouseButton) && grasped == null)
            {
                if (Input.GetMouseButtonDown(mouseButton)) // test if there is a graspable object under the cursor on this click. todo: key modifider for mac
                {
                    RaycastHit hit = new RaycastHit();
                    if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 100,
                                        Physics.DefaultRaycastLayers))
                    {
                        grasped = hit.collider.gameObject.GetComponentsInParent <MonoBehaviour>().Where(mb => mb is IGraspable).FirstOrDefault() as IGraspable;
                        if (grasped != null)
                        {
                            hand.MoveTo(hit.point);
                            previousProjectedHeight = GetProjectedHeight(hit.point);
                            grasped.Grasp(hand);
                        }
                    }
                }
            }

            if (Mathf.Abs(Input.mouseScrollDelta.y) > 0.01f)
            {
                transform.localPosition += (transform.localPosition - startingPosition) * Input.mouseScrollDelta.y * scrollScale;
            }

            if (grasped != null)
            {
                var projectedHeight = GetProjectedHeight(transform.position);
                var d = projectedHeight - previousProjectedHeight;
                previousProjectedHeight  = projectedHeight;
                transform.localPosition += new Vector3(0, d, 0);
            }
        }
Esempio n. 2
0
 public void Grasp(bool grasp)
 {
     if (grasp)
     {
         if (contacted != null)
         {
             // parent because physical bodies consist of a rigid body, and colliders *below* it in the scene graph
             grasped = contacted.gameObject.GetComponentsInParent <MonoBehaviour>().Where(mb => mb is IGraspable).FirstOrDefault() as IGraspable;
             grasped.Grasp(controller);
         }
     }
     else
     {
         if (grasped != null)
         {
             grasped.Release(controller);
             grasped = null;
         }
     }
 }