public void DisableInput() { #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER if (_nontouchInput == null) { _nontouchInput = GetComponent <NontouchInput>(); } _nontouchInput.Disable(); #elif (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR if (_touchInput == null) { _touchInput = GetComponent <TouchInput>(); } _touchInput.Disable(); #endif }
protected override void DeInit() { OnPressEvent = null; OnDragEvent = null; OnReleaseEvent = null; OnBackEvent = null; if (touchControls == null) { return; } touchControls.Touch.TouchPress.started -= OnPress; touchControls.Touch.TouchPress.canceled -= OnRelease; touchControls.Touch.TouchHold.started -= OnDrag; touchControls.Touch.Back.started -= OnBack; touchControls.Disable(); }
private void FixedUpdate() { var playerInput = Vector2.zero; if (!_dead && !_finished) { var gamepad = Gamepad.current; if (gamepad != null && _selectedInputMethod != InputMethod.Touch) { // Read onscreen gamepad playerInput = gamepad.leftStick.ReadValue(); if (_selectedInputMethod != InputMethod.Gamepad && playerInput.magnitude > 0.05f) { _touchInput.Disable(); _selectedInputMethod = InputMethod.Gamepad; } } // Read keyboard playerInput += new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); // Read touch input if (_touchInput.Started && _selectedInputMethod != InputMethod.Gamepad) { var distanceToTouchTarget = _touchInput.Target - _transform.position; if (distanceToTouchTarget.magnitude > .5f) { playerInput += new Vector2(distanceToTouchTarget.normalized.x, distanceToTouchTarget.normalized.z); } if (_selectedInputMethod != InputMethod.Touch) { Signals.Get <DisableOnScreenGamepad>().Dispatch(); _selectedInputMethod = InputMethod.Touch; } } if (!_firstInputReceived && playerInput.magnitude > .1f) { _firstInputReceived = true; Signals.Get <PlayerStartedControllingSignal>().Dispatch(); } playerInput = Vector2.ClampMagnitude(playerInput, 1f); _lastInput = playerInput; } if (_finished && _finishedAutoMoveTTL-- > 0) { // continue last input before finishing, but slow it down playerInput = _lastInput * Mathf.Lerp(.1f, 0, 1 - ((float)_finishedAutoMoveTTL / FinishedAutoMoveTTLNominal)); } Vector3 desiredVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * _maxSpeed; float maxSpeedChange = MaxAcceleration * Time.fixedDeltaTime; _velocity.x = Mathf.MoveTowards(_velocity.x, desiredVelocity.x, maxSpeedChange); _velocity.z = Mathf.MoveTowards(_velocity.z, desiredVelocity.z, maxSpeedChange); Vector3 displacement = _velocity * Time.fixedDeltaTime; Vector3 newPosition = _transform.localPosition + displacement; _transform.localPosition = newPosition; _stickContainer.eulerAngles = new Vector3(_velocity.z / _maxSpeed * MaxStickAngle, 0, _velocity.x / _maxSpeed * MaxStickAngle); }