public void Grab(GameObject anchor) // Vector3 newPosition, Vector3 newAngle)
    {
        print("Grabbing");
        grabbed = true;
        ownerController.networkDiskFired = false;
        anchorObj   = anchor;
        anchorTrans = anchor.transform;

        // If it's already grabbed by a hand...
        if (gameObject.transform.parent != null)
        {
            // Get it to release the disk
            HandGrabbing handScript = gameObject.transform.parent.GetComponent <HandGrabbing>();
            handScript.Release();
        }

        rb.velocity = new Vector3(0f, 0f, 0f);  // Stop the disk

        // Snap to hand
        // transform.position = anchor.transform.position; // newPosition;
        // transform.eulerAngles = anchor.transform.eulerAngles; // newAngle;
        // gameObject.transform.parent = anchor.transform.parent.transform;  // Set hand as parent
        // lastPosition = transform.position;

        // print("DiskController Hand position:" + anchor.transform.parent.transform.position);
        // print("DiskController Anchor position:" + anchor.transform.position);
    }
 public void SetParent(GameObject newParent)
 {
     if (gameObject.transform.parent != null)
     {
         HandGrabbing handScript = gameObject.transform.parent.GetComponent <HandGrabbing>();
         handScript.Release();
     }
     gameObject.transform.parent = newParent.transform;
 }
Exemple #3
0
    // Update is called once per frame
    void Update()
    {
        //update hand position and rotation
        if (isLocal)
        {
            transform.localPosition = InputTracking.GetLocalPosition(NodeType);
            transform.localRotation = InputTracking.GetLocalRotation(NodeType);
        }


        ///////////////
        // Added by Cam - 3/26/2018
        // Last updated - 4/2/2018
        //

        // Open-ended grabbing implementation

        GetInput();

        // Grabbing a disk
        if (triggerPress && !grabbing)
        {
            //check for colliders in proximity
            //Collider[] colliders = Physics.OverlapSphere(transform.position, GrabDistance);
            print(transform.name + ": " + "Disk (" + diskObj.transform.position + ") Hand (" + transform.position + ") :: " + Vector3.Distance(diskObj.transform.position, transform.position) + " <= " + GrabDistance + "?");
            if (Vector3.Distance(diskObj.transform.position, transform.position) <= GrabDistance)
            {
                print("attempting to grab");
                // If we collided with something
                //if (colliders.Length > 0)
                // {
                // If the collided object is a disk
                //if (colliders[0].transform.gameObject.tag == "Disk")
                // {
                //currentObject = colliders[0].transform;
                currentObject = diskObj.transform;
                //diskController = currentObject.gameObject.GetComponent<DiskController>();

                // If it's already being grabbed by your other hand, release it.
                if (currentObject.parent != null)
                {
                    HandGrabbing handScript = currentObject.transform.parent.GetComponent <HandGrabbing>();
                    handScript.Release();
                    // diskController.Release();  // Removed this because handScript.Release() should do this for us.
                }

                // print("HandGrabbing Hand position:" + gameObject.transform.position);
                // print("HandGrabbing Anchor position:" + anchor.position);

                diskController.Grab(anchor); // (second gameObject will be anchor) //gameObject.transform.position, gameObject.transform.eulerAngles);
                grabbing = true;
                //     }
                //}
            }
        }

        // Releasing a disk
        if (grabbing)
        {
            if (triggerRelease)
            {
                print("RELEASING");
                Release();
            }
        }

        //
        /////////

        /*  Original "gimme" code
         *
         * //if we don't have an active object in hand, look if there is one in proximity
         * if (currentObject == null)
         * {
         *  //check for colliders in proximity
         *  Collider[] colliders = Physics.OverlapSphere(transform.position, GrabDistance);
         *  if (colliders.Length > 0)
         *  {
         *      //if there are colliders, take the first one if we press the grab button and it has the tag for grabbing
         *      if (Input.GetAxis(InputName) >= 0.01f && colliders[0].transform.CompareTag(GrabTag))
         *      {
         *          //set current object to the object we have picked up
         *          currentObject = colliders[0].transform;
         *
         *          //if there is no rigidbody to the grabbed object attached, add one
         *          if(currentObject.GetComponent<Rigidbody>() == null)
         *          {
         *              currentObject.gameObject.AddComponent<Rigidbody>();
         *          }
         *
         *          //set grab object to kinematic (disable physics)
         *          currentObject.GetComponent<Rigidbody>().isKinematic = true;
         *
         *
         *      }
         *  }
         * }
         * else
         * //we have object in hand, update its position with the current hand position (+defined offset from it)
         * {
         *  currentObject.position = transform.position + ObjectGrabOffset;
         *
         *  //if we we release grab button, release current object
         *  if (Input.GetAxis(InputName) < 0.01f)
         *  {
         *      //set grab object to non-kinematic (enable physics)
         *      Rigidbody _objectRGB = currentObject.GetComponent<Rigidbody>();
         *      _objectRGB.isKinematic = false;
         *
         *      //calculate the hand's current velocity
         *      Vector3 CurrentVelocity = (transform.position - _lastFramePosition) / Time.deltaTime;
         *
         *      //set the grabbed object's velocity to the current velocity of the hand
         *      _objectRGB.velocity = CurrentVelocity * ThrowMultiplier;
         *
         *      //release the reference
         *      currentObject = null;
         *  }
         *
         * }
         *
         * //save the current position for calculation of velocity in next frame
         * _lastFramePosition = transform.position;
         *
         */
        //diskController.Grab(anchor); // (second gameObject will be anchor) //gameObject.transform.position, gameObject.transform.eulerAngles);
        //grabbing = true;
    }
Exemple #4
0
 // Use this for initialization
 void Start()
 {
     _anim     = GetComponentInChildren <Animator>();
     _handGrab = GetComponent <HandGrabbing>();
 }