//------------------------------------------------------------ // Methods for InteractableLever //------------------------------------------------------------ #region METHODS void Awake() { _hoverFrame = 0; _isDown = false; _isHover = false; _enabled = true; _angle = 0f; _targetAngle = 0f; _dragAngleOffset = 0f; _lastOutput = null; if (snapAngles != null && snapAngles.Length > 0) { //the current up-vector for the lever Vector3 up = lever.rotation * Vector3.up; //the origin up-vector, according to the rotation of the base Vector3 axisUp = leverAxis.rotation * Vector3.up; //the current real angle the lever is at float startingAngle = SignedDegreesBetween(up, axisUp, leverAxis.rotation * Vector3.forward); _currentSnapAngle = GetClosestLeverAngle(startingAngle); } else { _currentSnapAngle = null; } }
protected virtual void Update() { bool gvrClicked = GvrController.ClickButtonDown; bool gvrDown = GvrController.ClickButton; //if we stopped hovering, invoke the proper event(s) and do global stuff { bool previouslyHovering = _isHover; _isHover = (_hoverFrame--) > 0; if (!_isHover && GlobalHoverTarget == this) { GlobalHoverTarget = null; } if (previouslyHovering && !_isHover && !_isDown && events.onHoverExit != null) { events.onHoverExit.Invoke(); } } //allow dragging dragging, even if we're not hovering, so long as the button was pressed when we were previously hovering over the object if (_isHover && gvrClicked) { //if we were previously released, invoke ClickPressed if (!_isDown) { ClickPressed(); } _isDown = true; } if (!gvrDown) { if (_isDown) { ClickReleased(); } _isDown = false; } var prevSnap = _currentSnapAngle; _currentSnapAngle = GetLeverAngleToSnapTo(_targetAngle) ?? _currentSnapAngle; //if we switched angle-snaps, invoke the appropriate events if (prevSnap != _currentSnapAngle) { _invokedSnap = false; } if (!_invokedSnap && _currentSnapAngle != null && _currentSnapAngle.onLockedToAngle != null && !(_isDown & _currentSnapAngle.requireReleaseForEvents) && Mathf.Abs(Mathf.DeltaAngle(_angle, _currentSnapAngle.angle)) <= _currentSnapAngle.triggerAngleDifference) { _invokedSnap = true; _currentSnapAngle.onLockedToAngle.Invoke(); } if (_isDown) { UpdateLeverAngle(PointerPosition, PointerEulers); if (connectedLevers != null) { for (int c = 0; c < connectedLevers.Length; ++c) { if (connectedLevers[c].lever != null) { connectedLevers[c].lever.Angle = this.Angle * connectedLevers[c].ratio; } } } } else { if (_currentSnapAngle != null) { _targetAngle = _currentSnapAngle.angle; _angle = Mathf.LerpAngle(_angle, _targetAngle, rotationSpeed); //set the rotation on the lever to point at the hit vector lever.rotation = leverAxis.rotation * Quaternion.AngleAxis(_angle, Vector3.forward); } } float output = Output; if (_lastOutput != null) { if (events.onOutputChanged != null) { events.onOutputChanged.Invoke(output); } } if (animator != null) { animator.SetBool(ANIM_PROP_HOVER, _isHover); animator.SetBool(ANIM_PROP_DOWN, _isDown); } }