private void HandleCharacterInput() { PlayerCharacterInputs characterInputs = new PlayerCharacterInputs(); // Build the CharacterInputs struct characterInputs.MoveAxisForward = Input.GetAxisRaw(VerticalInput); characterInputs.MoveAxisRight = Input.GetAxisRaw(HorizontalInput); characterInputs.CameraRotation = CharacterCamera.Transform.rotation; characterInputs.JumpDown = Input.GetKeyDown(KeyCode.Space); characterInputs.CrouchDown = Input.GetKey(KeyCode.LeftControl); characterInputs.CrouchUp = Input.GetKeyUp(KeyCode.LeftControl); characterInputs.RunStart = Input.GetKeyDown(KeyCode.LeftShift); characterInputs.RunStop = Input.GetKeyUp(KeyCode.LeftShift); characterInputs.SwordSwing = Input.GetMouseButtonDown(0); characterInputs.Interact = Input.GetKeyDown(KeyCode.E); // Apply inputs to character Character.SetInputs(ref characterInputs); }
/// <summary> /// This is called every frame by ExamplePlayer in order to tell the character what its inputs are /// </summary> public void SetInputs(ref PlayerCharacterInputs inputs) { // Clamp input Vector3 moveInputVector = Vector3.ClampMagnitude(new Vector3(inputs.MoveAxisRight, 0f, inputs.MoveAxisForward), 1f); // Calculate camera direction and rotation on the character plane Vector3 cameraPlanarDirection = Vector3.ProjectOnPlane(inputs.CameraRotation * Vector3.forward, Motor.CharacterUp).normalized; if (cameraPlanarDirection.sqrMagnitude == 0f) { cameraPlanarDirection = Vector3.ProjectOnPlane(inputs.CameraRotation * Vector3.up, Motor.CharacterUp).normalized; } Quaternion cameraPlanarRotation = Quaternion.LookRotation(cameraPlanarDirection, Motor.CharacterUp); // Move and look inputs _moveInputVector = cameraPlanarRotation * moveInputVector; switch (OrientationMethod) { case OrientationMethod.TowardsCamera: _lookInputVector = cameraPlanarDirection; break; case OrientationMethod.TowardsMovement: _lookInputVector = _moveInputVector.normalized; break; } switch (CurrentCharacterState) { case CharacterState.Default: { // Jumping input if (inputs.JumpDown) { _timeSinceJumpRequested = 0f; _jumpRequested = true; } // Crouching input if (inputs.CrouchDown) { _shouldBeCrouching = true; if (!_isCrouching.value) { _isCrouching.value = true; TransitionToState(CharacterState.Crouched); Motor.SetCapsuleDimensions(0.5f, 1f, 0.5f); MeshRoot.localScale = new Vector3(1f, 0.5f, 1f); } } if (inputs.RunStart && Motor.GroundingStatus.IsStableOnGround) { TransitionToState(CharacterState.Running); } break; } case CharacterState.Crouched: { if (inputs.CrouchUp) { _shouldBeCrouching = false; } break; } case CharacterState.Running: { if (inputs.RunStop) { TransitionToState(CharacterState.Default); } break; } } }