public void SetCursorConstraints( string constraintsName) { int newCursorConstraintIndex = FindCursorConstraints(constraintsName); if (newCursorConstraintIndex == -1) { Debug.LogError( string.Format("VRCursor: Unable to find constraints {0}, using {1} instead", constraintsName, DEFAULT_CURSOR_CONSTRAINTS)); newCursorConstraintIndex = m_defaultCursorConstraintsIndex; } if (newCursorConstraintIndex != m_activeCursorConstraintsIndex) { CursorConstraints oldConstraints = CursorConstraintList[m_activeCursorConstraintsIndex]; CursorConstraints newConstraints = CursorConstraintList[newCursorConstraintIndex]; // If the cursor is still in the XZ plane and becomes disallowed to be there, // snap the cursor to the XY plane and re-orient if (CursorPlane == eCursorPlane.XZPlane && oldConstraints.AllowXZPlane && !newConstraints.AllowXZPlane) { Vector3 snappedCursorPosition = this.transform.position; snappedCursorPosition.z = newConstraints.ZMax; this.transform.position = snappedCursorPosition; this.transform.rotation = XY_ORIENTATION; this.CursorPlane = eCursorPlane.XYPlane; } // Make sure the cursor position is inside the new bounds { Vector3 desiredCursorPosition = this.transform.position; desiredCursorPosition = newConstraints.SnapPositionInsideBounds(desiredCursorPosition); this.transform.position = desiredCursorPosition; } m_activeCursorConstraintsIndex = newCursorConstraintIndex; } }
void Update() { CursorConstraints constraints = GetCursorConstraints(); Vector3 currentCursorPosition = this.transform.position; Vector3 desiredCursorPosition = currentCursorPosition; Quaternion desiredCursorOrientation = this.transform.rotation; Vector3 mouseDelta; mouseDelta.x = Input.GetAxis("Mouse X"); mouseDelta.y = Input.GetAxis("Mouse Y"); mouseDelta.z = 0.0f; // Keep the mouse hidden and locked Screen.showCursor = false; Screen.lockCursor = true; // Remember where the cursor was before it moves m_lastCursorPlane = CursorPlane; m_lastCursorPosition = currentCursorPosition; // Move the cursor switch (CursorPlane) { case eCursorPlane.XYPlane: { desiredCursorPosition.x = currentCursorPosition.x + mouseDelta.x * constraints.XYSpeedScale; desiredCursorPosition.y = currentCursorPosition.y + mouseDelta.y * constraints.XYSpeedScale; if (desiredCursorPosition.y < constraints.YMin && constraints.AllowXZPlane) { // Fold the remaining downward motion onto the XZ plane desiredCursorPosition.z = constraints.ZMax - (constraints.YMin - desiredCursorPosition.y); desiredCursorPosition.y = constraints.YMin; // Make the cursor perpendicular to the XZ plane desiredCursorOrientation = XZ_ORIENTATION; // Keep track of the cursor plane change CursorPlane = eCursorPlane.XZPlane; } } break; case eCursorPlane.XZPlane: { desiredCursorPosition.x = currentCursorPosition.x + mouseDelta.x * constraints.XZSpeedScale; desiredCursorPosition.z = currentCursorPosition.z + mouseDelta.y * constraints.XZSpeedScale; if (desiredCursorPosition.z > constraints.ZMax && constraints.AllowXZPlane) { // Fold the remaining Z+ motion onto the XY plane desiredCursorPosition.y = constraints.YMin + (desiredCursorPosition.z - constraints.ZMax); desiredCursorPosition.z = constraints.ZMax; // Make the cursor perpendicular to the XZ plane desiredCursorOrientation = XY_ORIENTATION; // Keep track of the cursor plane change CursorPlane = eCursorPlane.XYPlane; } } break; } // Keep cursor position in bounds at all times desiredCursorPosition = constraints.SnapPositionInsideBounds(desiredCursorPosition); // Update cursor position and orientation this.transform.position = desiredCursorPosition; this.transform.rotation = desiredCursorOrientation; }