Example #1
0
    private void Rpc_ClientRelease(bool isLeftController, Vector3 velocity, Vector3 angularVelocity)
    {
        //Set controller based on which called
        GameObject attachPoint;

        SteamVR_Controller.Device device;
        if (isLeftController)
        {
            attachPoint = leftControllerAttachPoint;
            device      = Steam_LeftController;
            if (leftLastThrownDiscTime + discRespawnTime < Time.time)
            {
                leftLastThrownDiscTime = Time.time;
            }
        }
        else
        {
            attachPoint = rightControllerAttachPoint;
            device      = Steam_RightController;
            if (rightLastThrownDiscTime + discRespawnTime < Time.time)
            {
                rightLastThrownDiscTime = Time.time;
            }
        }

        FixedJoint joint = attachPoint.GetComponent <FixedJoint>();

        if (joint != null)
        {
            if (joint.connectedBody == null)
            {
                DestroyImmediate(joint);
                return;
            }
            //Retrieve the interactable from the object
            GameObject releasedGo = joint.connectedBody.gameObject;
            InteractablePhysicalObject releasedInteractable = releasedGo.GetComponent <InteractablePhysicalObject>();
            //Debug.Log("releasedInteractable=" + releasedInteractable);
            //Destroy the joint connecting the two gameObjects 'releasing' the object
            releasedInteractable.beingInteracted = false;
            DestroyImmediate(joint);

            joint = null;

            //Apply physics to the interactable as if it were carrying momentum if there were any from the attachpoint
            float velocityMultiplier, angularVelocityMultiplier;
            velocityMultiplier        = releasedInteractable.throwMultiplier;
            angularVelocityMultiplier = releasedInteractable.angularThrowMultiplier;

            //Apply a force to the disc
            Rigidbody rb = releasedInteractable.GetComponent <Rigidbody>();

            //Debug.Log("velocity=" + velocity);

            rb.velocity        = velocity * velocityMultiplier;
            rb.angularVelocity = angularVelocity * angularVelocityMultiplier;
        }
    }
Example #2
0
    private void Rpc_ClientGrab(bool isLeftController, NetworkInstanceId discID)
    {
        //Debug.Log("Server told me to grab");
        //Set controller based on which called
        GameObject attachPoint;

        if (isLeftController)
        {
            attachPoint = leftControllerAttachPoint;
        }
        else
        {
            attachPoint = rightControllerAttachPoint;
        }

        //Get list of interactables in the environment
        InteractablePhysicalObject[] interactables = GameObject.FindObjectsOfType(typeof(InteractablePhysicalObject)) as InteractablePhysicalObject[];

        //Search for given netID
        InteractablePhysicalObject spawnedInteractable = null;

        foreach (InteractablePhysicalObject ipo in interactables)
        {
            if (ipo.GetComponent <NetworkIdentity>().netId == discID)
            {
                spawnedInteractable = ipo;
                break;
            }
        }

        if (spawnedInteractable != null)
        {
            //Debug.Log("Found spawned Interactable");
            // Flag interactable as active.
            spawnedInteractable.beingInteracted = true;

            // Set the position of the interactable and joint to rigidbody attachpoint on controllers
            spawnedInteractable.gameObject.transform.position = attachPoint.transform.position + spawnedInteractable.grabOffsetPosition;
            spawnedInteractable.gameObject.transform.rotation = attachPoint.transform.rotation * Quaternion.Euler(spawnedInteractable.grabOffsetRotation);

            //Add a joint onto the attachpoint
            FixedJoint joint = attachPoint.AddComponent <FixedJoint>();

            //Connect it to the interactable's rigidbody
            joint.connectedBody = spawnedInteractable.gameObject.GetComponent <Rigidbody>();
        }
    }