Beispiel #1
0
    void Start()
    {
        colliding = false;
        grounded  = false;

        // Second substitution, because RUISKinectAndMecanimCombiner might have already erased the original one and re-created it
        skeletonController = gameObject.GetComponentInChildren <RUISSkeletonController>();
        if (skeletonController)
        {
            bodyParts.Add(skeletonController.leftFoot);
            bodyParts.Add(skeletonController.rightFoot);
            kinectPlayerId       = skeletonController.playerId;
            bodyTrackingDeviceID = skeletonController.bodyTrackingDeviceID;
        }
        else
        {
            Debug.LogError("RUISCharacterController script in game object '" + gameObject.name
                           + "' did not find RUISSkeletonController component from it's child objects!");
        }

        coordinateSystem = FindObjectOfType(typeof(RUISCoordinateSystem)) as RUISCoordinateSystem;

        if (stabilizingCollider)
        {
            colliderComponent = stabilizingCollider.gameObject.GetComponent <Collider>();
            if (colliderComponent)
            {
                if (characterPivotType == CharacterPivotType.KinectHead ||
                    characterPivotType == CharacterPivotType.KinectTorso)
                {
                    if (coordinateSystem && (inputManager.enableKinect || inputManager.enableKinect2) && !coordinateSystem.setKinectOriginToFloor)
                    {
                        Debug.LogWarning("It is best to enable 'setKinectOriginToFloor' from RUISCoordinateSystem " +
                                         "when using Kinect and RUISCharacterController script.");
                    }
                }

                if (colliderComponent.material)
                {
                    originalMaterial = colliderComponent.material;
                }
                else
                {
                    colliderComponent.material = new PhysicMaterial();
                    originalMaterial           = colliderComponent.material;
                }

                if (dynamicMaterial == null)
                {
                    dynamicMaterial = new PhysicMaterial();

                    dynamicMaterial.dynamicFriction = 0;
                    dynamicMaterial.staticFriction  = 0;
                    dynamicMaterial.frictionCombine = PhysicMaterialCombine.Minimum;

                    if (colliderComponent.material)
                    {
                        dynamicMaterial.bounceCombine = originalMaterial.bounceCombine;
                        dynamicMaterial.bounciness    = originalMaterial.bounciness;
//						dynamicMaterial.staticFriction2 = originalMaterial.staticFriction2; // Deprecated since Unity 5.2ish
//						dynamicMaterial.dynamicFriction2 = originalMaterial.dynamicFriction2;
//						dynamicMaterial.frictionDirection2 = originalMaterial.frictionDirection2;
                    }
                }
            }
        }
        if ((characterPivotType == CharacterPivotType.KinectHead ||
             characterPivotType == CharacterPivotType.KinectTorso) &&
            (skeletonController && skeletonController.playerId != kinectPlayerId))
        {
            Debug.LogError("The 'Kinect Player Id' variable in RUISCharacterController script in gameObject '" + gameObject.name
                           + "is different from the Kinect Player Id of the RUISSkeletonController script (located in child "
                           + "object '" + skeletonController.gameObject.name + "). Make sure that these two values are "
                           + "the same.");
        }

        if (useHmdPositionalTracking && !RUISDisplayManager.IsHmdPositionTrackable())
        {
            Debug.LogError("Position tracked head-mounted display is not found, can't use it as a character pivot!");
        }

//		try
//		{
//			bool isRiftConnected = false;
//			if(OVRManager.display != null)
//				isRiftConnected = OVRManager.display.isPresent; //06to08
//			if(OVRManager.capiHmd != null)
//				ovrHmdVersion = OVRManager.capiHmd.GetDesc().Type; //06to08
//
//			if(useOculusPositionalTracking && ovrHmdVersion == Ovr.HmdType.DK1 || ovrHmdVersion == Ovr.HmdType.DKHD || ovrHmdVersion == Ovr.HmdType.None) //06to08
//			{
//				Debug.LogError("Can't use Oculus Rift's tracked position as a pivot with Oculus Rift " + ovrHmdVersion);
//				useOculusPositionalTracking = false;
//			}
//
//			if(useOculusPositionalTracking && !isRiftConnected)
//			{
//				Debug.LogError("Can't use Oculus Rift's tracked position as a pivot because Oculus Rift is not connected.");
//				useOculusPositionalTracking = false;
//			}
//
//		}
//		catch(UnityException e)
//		{
//			useOculusPositionalTracking = false;
//			Debug.LogError(e);
//		}

        if (GetComponentInChildren <RUISKinectAndMecanimCombiner>())
        {
            kinectAndMecanimCombinerExists = true;
        }

        previousPosition = transform.position;
    }
    void Awake()
    {
        string[] names = System.Enum.GetNames(typeof(RUISDevice));
        foreach (string device in names)
        {
            RUISDevice device1Enum = (RUISDevice)System.Enum.Parse(typeof(RUISDevice), device, true);
            RUISCalibrationResultsFloorPitchRotation[device1Enum] = Quaternion.identity;
            RUISCalibrationResultsDistanceFromFloor[device1Enum]  = 0.0f;
            foreach (string device2 in names)
            {
                if (device != device2)
                {
                    string devicePairString = device + "-" + device2;
                    RUISCalibrationResultsInVector3[devicePairString]    = new Vector3(0, 0, 0);
                    RUISCalibrationResultsInQuaternion[devicePairString] = Quaternion.identity;
                    RUISCalibrationResultsIn4x4Matrix[devicePairString]  = Matrix4x4.identity;
                }
            }
        }

        if (loadFromXML)
        {
            if (!LoadMultiXML(coordinateXmlFile))
            {
                createExampleXML(coordinateXmlFile);
            }
        }

        inputManager = FindObjectOfType <RUISInputManager>();
        if (switchToAvailableDevice && inputManager)
        {
            bool       needToSwitch   = false;
            RUISDevice previousDevice = rootDevice;

            switch (rootDevice)
            {
            case RUISDevice.Kinect_1:
                if (!inputManager.enableKinect)
                {
                    needToSwitch = true;
                }
                break;

            case RUISDevice.Kinect_2:
                if (!inputManager.enableKinect2)
                {
                    needToSwitch = true;
                }
                break;

            case RUISDevice.PS_Move:
                if (!inputManager.enablePSMove)
                {
                    needToSwitch = true;
                }
                break;

            case RUISDevice.OpenVR:             // If OpenVR can't accessed AND a HMD can't be detected
                if (!RUISDisplayManager.IsOpenVrAccessible() && !RUISDisplayManager.IsHmdPresent())
                {
                    needToSwitch = true;
                }
                break;
            }

            if (needToSwitch)
            {
                // Try to determine if Kinect2 can be used (because this method is run before RUISInputManager can disable enableKinect2)
                bool kinect2FoundBySystem = false;
                if (inputManager.enableKinect2)
                {
                    try
                    {
                        Kinect2SourceManager kinect2SourceManager = FindObjectOfType(typeof(Kinect2SourceManager)) as Kinect2SourceManager;
                        if (kinect2SourceManager != null && kinect2SourceManager.GetSensor() != null && kinect2SourceManager.GetSensor().IsOpen)
                        {
                            // IsOpen seems to return false mostly if Kinect 2 drivers are not installed?
                            //					Debug.Log("Kinect 2 was detected by the system.");
                            kinect2FoundBySystem = true;
                        }
                    }
                    catch
                    {}
                }

                if (RUISDisplayManager.IsHmdPositionTrackable())
                {
                    rootDevice = RUISDevice.OpenVR;
                }
                else if (inputManager.enableKinect2 && kinect2FoundBySystem)
                {
                    rootDevice = RUISDevice.Kinect_2;
                }
                else if (inputManager.enableKinect)
                {
                    rootDevice = RUISDevice.Kinect_1;
                }
                else if (inputManager.enablePSMove)
                {
                    rootDevice = RUISDevice.PS_Move;
                }

                if (rootDevice != previousDevice)
                {
                    if (previousDevice == RUISDevice.OpenVR)
                    {
                        Debug.LogWarning("Switched 'Master Coordinate System Sensor' from " + previousDevice + " to " + rootDevice + " "
                                         + "because OpenVR could not be accessed! Is SteamVR installed?");
                    }
                    else
                    {
                        Debug.LogWarning("Switched 'Master Coordinate System Sensor' from " + previousDevice + " to " + rootDevice + " "
                                         + "because the former was not enabled in " + typeof(RUISInputManager) + " while the latter was!");
                    }
                }
            }
        }
    }
Beispiel #3
0
    private Vector3 GetPivotPositionInTrackerCoordinates()
    {
        if (useHmdPositionalTracking && RUISDisplayManager.IsHmdPositionTrackable() && coordinateSystem)
        {
            if (coordinateSystem.applyToRootCoordinates)
            {
                return(coordinateSystem.ConvertLocation(coordinateSystem.GetHmdRawPosition(), RUISDevice.OpenVR));
            }
            else
            {
                return(coordinateSystem.GetHmdRawPosition());
            }
        }
        else
        {
            switch (characterPivotType)
            {
            case CharacterPivotType.KinectHead:
            {
                if ((bodyTrackingDeviceID == RUISSkeletonManager.kinect2SensorID && !inputManager.enableKinect2) ||
                    (bodyTrackingDeviceID == RUISSkeletonManager.kinect1SensorID && !inputManager.enableKinect))
                {
                    break;
                }

                if (skeletonManager && skeletonManager.skeletons[bodyTrackingDeviceID, kinectPlayerId] != null)
                {
                    if (skeletonController)                            // Add root speed scaling
                    {
                        return(Vector3.Scale(skeletonManager.skeletons[bodyTrackingDeviceID, kinectPlayerId].head.position, skeletonController.rootSpeedScaling));
                    }
                    else
                    {
                        return(skeletonManager.skeletons[bodyTrackingDeviceID, kinectPlayerId].head.position);
                    }
                }
                break;
            }

            case CharacterPivotType.KinectTorso:
            {
                if ((bodyTrackingDeviceID == RUISSkeletonManager.kinect2SensorID && !inputManager.enableKinect2) ||
                    (bodyTrackingDeviceID == RUISSkeletonManager.kinect1SensorID && !inputManager.enableKinect))
                {
                    break;
                }

                if (skeletonManager && skeletonManager.skeletons[bodyTrackingDeviceID, kinectPlayerId] != null)
                {
                    if (skeletonController)                            // Add root speed scaling
                    {
                        return(Vector3.Scale(skeletonManager.skeletons[bodyTrackingDeviceID, kinectPlayerId].torso.position, skeletonController.rootSpeedScaling));
                    }
                    else
                    {
                        return(skeletonManager.skeletons[bodyTrackingDeviceID, kinectPlayerId].torso.position);
                    }
                }
                break;
            }

            case CharacterPivotType.MoveController:
            {
                if (!inputManager.enablePSMove)
                {
                    break;
                }

                //if(inputManager.GetMoveWand(moveControllerId))
                //	return inputManager.GetMoveWand(moveControllerId).handlePosition;
                break;
            }
            }
        }

        if (skeletonController != null)
        {
            return(skeletonController.transform.localPosition);
        }
        else
        {
            return(Vector3.zero);
        }
    }