Beispiel #1
0
    void FixedUpdate()
    {
        //setup ray cast to get object what was hit by the ray
        Vector3    directionFwd = transform.TransformDirection(Vector3.forward); //direction of character
        RaycastHit hit;
        GameObject objectHit = null;


        if (Physics.Raycast(transform.position, directionFwd, out hit, grabDistance))
        {
            //get object that was hit
            objectHit = hit.collider.gameObject;
            InstructionCanvasController icc = objectHit.GetComponent <InstructionCanvasController>();

            if (icc != null && icc.HasSign == false && !Input.GetMouseButton(0))
            {
                //add a sign to that object
                icc.createSign("Tennisball", 0.5f, transform.rotation);
            }

            if (objectHit.CompareTag("Throwable"))
            {
                target = objectHit;
            }
        }



        if (Input.GetMouseButton(0) && target != null && !Input.GetMouseButtonDown(1))
        {
            InstructionCanvasController icc = target.GetComponent <InstructionCanvasController>();
            icc.disableSign();

            //detach grabbed Object from Physics engine
            target.GetComponent <Rigidbody>().isKinematic = true;
            target.transform.position = new Vector3(grabbedObject.transform.position.x, grabbedObject.transform.position.y, grabbedObject.transform.position.z);
            target.transform.rotation = transform.rotation;
        }
        else if (Input.GetMouseButtonUp(0) && target != null)
        {
            InstructionCanvasController icc = target.GetComponent <InstructionCanvasController>();
            icc.disableSign();

            //attach grabbed Object from Physics engine
            Rigidbody rb = target.GetComponent <Rigidbody>();
            rb.isKinematic = false;

            //get velocity of object grabbed
            applyForce(rb);
        }
    }
Beispiel #2
0
    private void FixedUpdate()
    {
        /**
         * Raycast for signs to pop up
         */
        Vector3    fwd = transform.TransformDirection(Vector3.forward);
        RaycastHit hit;
        GameObject hitObject;

        if (Physics.Raycast(transform.position, fwd, out hit, grabDistance))
        {
            //get object that was hit
            hitObject = hit.collider.gameObject;
            InstructionCanvasController icc = hitObject.GetComponent <InstructionCanvasController>();

            if (icc != null && icc.HasSign == false && !Input.GetMouseButton(0))
            {
                //add a sign to that object
                icc.createSign("Pick up the ball", 0.5f, transform.rotation);
            }

            if (hitObject.CompareTag("Throwable") || hitObject.CompareTag("MenuItem"))
            {
                target = hitObject;


                /***
                 * SteamVR Throw object code
                 */
                var device = SteamVR_Controller.Input((int)trackedObj.index);
                if (joint == null && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
                {
                    //TODO: make prefab selectable from UI on left Controller


                    try
                    {
                        GameObject go;
                        //create new object from grabbed object
                        go = Instantiate(getPrefabFromLayer(target.layer));

                        go.transform.position = attachPoint.transform.position;

                        joint = go.AddComponent <FixedJoint>();
                        joint.connectedBody = attachPoint;

                        if (target.CompareTag("MenuItem"))
                        {
                            //DestroyImmediate(target);
                        }
                        else
                        {
                            DestroyImmediate(target);
                        }
                    } catch (Exception e)
                    {
                        Debug.LogError(e.ToString());
                    }
                }
                //TODO: fix on button up throwing object
                else if (joint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
                {
                    target = joint.gameObject;
                    Rigidbody rb = target.GetComponent <Rigidbody>();
                    DestroyImmediate(joint);
                    joint = null;


                    // We should probably apply the offset between trackedObj.transform.position
                    // and device.transform.pos to insert into the physics sim at the correct
                    // location, however, we would then want to predict ahead the visual representation
                    // by the same amount we are predicting our render poses.

                    var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
                    if (origin != null)
                    {
                        rb.velocity        = origin.TransformVector(device.velocity);
                        rb.angularVelocity = origin.TransformVector(device.angularVelocity);
                    }
                    else
                    {
                        rb.velocity        = device.velocity;
                        rb.angularVelocity = device.angularVelocity;
                    }

                    rb.maxAngularVelocity = rb.angularVelocity.magnitude;
                }
            }
        }
    }