// Movement is handled in the fixed update private void FixedUpdate() { // Get the Virtualizer device, open it, and begin reading data from it if (m_Device != null) { if (!m_Device.IsOpen()) { print("[Opening Virtualizer...]"); m_Device.Open(); } m_MoveSpeed = PlayerPrefs.GetFloat("MoveSpeed"); m_SensorThreshold = PlayerPrefs.GetFloat("MoveSens"); int direction = (int)m_Device.GetDirectionRaw(); float speed = m_Device.GetMovementSpeed(); GroundCheck(); if ((direction == 0 || direction == 1) && speed > m_SensorThreshold) { m_RigidBody.Sleep(); Vector3 forwardMove = bodyRotation.transform.forward; forwardMove = Vector3.ProjectOnPlane(forwardMove, m_GroundContactNormal).normalized; forwardMove.x = forwardMove.x * m_MoveSpeed; forwardMove.z = forwardMove.z * m_MoveSpeed; forwardMove.y = forwardMove.y * m_MoveSpeed; if (m_RigidBody.velocity.sqrMagnitude < m_MoveSpeed) { Debug.LogWarning("ADDING FORCE!"); m_RigidBody.AddForce(forwardMove, ForceMode.Impulse); } } else { m_RigidBody.Sleep(); } } }
// Update is called once per frame void Update() { if (deviceController != null) { CVirtDevice virtDevice = deviceController.GetDevice(); if (virtDevice != null) { // Get Virtualizer raw inputs ///////////////////////////// Vector3 virtOrientation = virtDevice.GetPlayerOrientation(); /*float virtHeight = virtDevice.GetPlayerHeight();*/ Vector3 virtDirection = virtDevice.GetMovementDirection(); float virtSpeed = virtDevice.GetMovementSpeed(); // Turn /////// Quaternion rotation = new Quaternion(); rotation.SetLookRotation(virtOrientation, Vector3.up); transform.localRotation = rotation; //TODO: Crouch ///////// // Move Character ///////////////// if (virtSpeed != 0.0f) { virtSpeed = virtSpeed * movementSpeedMultiplier; if (characterController != null) { this.characterController.SimpleMove(transform.TransformDirection(virtDirection * virtSpeed)); } } } } }
// Update is called once per frame void Update() { float playerHeight = 0.0f; if (deviceController != null) { CVirtDevice virtDevice = deviceController.GetDevice(); if (virtDevice != null) { // Get Virtualizer raw inputs ///////////////////////////// Vector3 virtOrientation = virtDevice.GetPlayerOrientation(); float virtHeight = virtDevice.GetPlayerHeight(); Vector3 virtDirection = virtDevice.GetMovementDirection(); float virtSpeed = virtDevice.GetMovementSpeed(); // Turn /////// Quaternion rotation = new Quaternion(); rotation.SetLookRotation(virtOrientation, Vector3.up); forwardDirection.localRotation = rotation; // Hip Height ///////// playerHeight = virtHeight / 100.0f; // Move Character ///////////////// if (virtSpeed != 0.0f) { virtSpeed = virtSpeed * movementSpeedMultiplier; if (characterController != null) { this.characterController.SimpleMove((forwardDirection.TransformDirection(virtDirection)).normalized * virtSpeed); } } } } if (useProfileHeight) { if (initialPose == null) { initialPose = new OVRPose() { position = cameraController.transform.localPosition, orientation = cameraController.transform.localRotation }; } var p = cameraController.transform.localPosition; p.y = (OVRManager.profile.eyeHeight - 0.5f * characterController.height) + playerHeight; p.z = OVRManager.profile.eyeDepth; cameraController.transform.localPosition = p; } else if (initialPose != null) { cameraController.transform.localPosition = initialPose.Value.position; cameraController.transform.localRotation = initialPose.Value.orientation; initialPose = null; } }
// Update is called once per frame void Update() { bool executeBaseUpdate = true; if (this.deviceController != null) { CVirtDevice device = this.deviceController.GetDevice(); if (device != null) { if (device.IsOpen() == false) { device.Open(); } else { #if !UNITY_EDITOR executeBaseUpdate = false; #endif // MOVE /////////// Vector3 input = device.GetMovementDirection(); input *= device.GetMovementSpeed(); // ROTATION /////////// Vector3 newForward = device.GetPlayerOrientation(); if (this.forwardDirection != null) { this.forwardDirection.transform.localRotation = Quaternion.LookRotation(newForward, Vector3.up); } // Get the forward direction Vector3 f = forwardDirection.forward; f.y = 0f; // If not dead, move if (this.movementSpeedMultiplier != 0f) { characterController.SimpleMove(Quaternion.LookRotation(f) * (input * this.movementSpeedMultiplier) + 0.1f * Vector3.down); } else { characterController.SimpleMove(Vector3.zero); } } } } // When a Virtualizer isn't plugged and we want // to test with keyboard/mouse for example /////////////////////////////////////////////// if (executeBaseUpdate == true) { Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); // Find the forward direction Vector3 f = forwardDirection.forward; f.y = 0f; characterController.SimpleMove(Quaternion.LookRotation(f) * Vector3.ClampMagnitude(input, 1f) * this.movementSpeedMultiplier * (Input.GetKey(KeyCode.LeftShift) ? 3.0F : 1.0F)); //@Cyberith: Run if (Input.GetKeyDown(KeyCode.E)) { transform.rotation = Quaternion.Euler(0f, 45f, 0f) * transform.rotation; } else if (Input.GetKeyDown(KeyCode.Q)) { transform.rotation = Quaternion.Euler(0f, -45f, 0f) * transform.rotation; } transform.rotation = Quaternion.Euler(0f, Input.GetAxis("Mouse X") * 2f, 0f) * transform.rotation; } }