// Use this for initialization void Start() { #if UNITY_EDITOR || UNITY_STANDALONE_WIN switch (deviceTypeIndex) { //Auto selection case 0: virtDevice = getStandardVirtualizerDevice(); if (virtDevice == null) { fallbackToStandardCoupled(); return; } break; //Standard de-coupled virtualizer input case 1: virtDevice = getStandardVirtualizerDevice(); break; //TODO: Standard coupled movement input case 2: fallbackToStandardCoupled(); return; //Faild to find VirtDevice default: break; } #endif #if UNITY_ANDROID virtDevice = new CVirtDeviceBluetooth(); #endif if (virtDevice != null) { CLogger.Log("Virtualizer device found, connecting..."); if (virtDevice.Open()) { CLogger.Log("Successfully connected to Virtualizer device."); //Reset ResetPlayerOrientation and PlayerHeight virtDevice.ResetPlayerOrientation(); virtDevice.GetPlayerHeight(); } else { CLogger.LogError("Failed to connect to Virtualizer device."); } } else { CLogger.LogError("Virtualizer device not found..."); } }
// 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() { 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; } }