Esempio n. 1
0
        private void HandleCharacterInput()
        {
            PlayerCharacterInputs characterInputs = new PlayerCharacterInputs();

            // Build the CharacterInputs struct
            characterInputs.MoveAxisForward = Input.GetAxisRaw(VerticalInput);
            characterInputs.MoveAxisRight   = Input.GetAxisRaw(HorizontalInput);
            // characterInputs.CameraRotation = OrbitCamera.Transform.rotation;
            //characterInputs.JumpDown = Input.GetKeyDown(KeyCode.Space);
            characterInputs.CrouchDown          = Input.GetKey(KeyCode.LeftControl);
            characterInputs.CrouchUp            = Input.GetKeyUp(KeyCode.LeftControl);
            characterInputs.SwordSwing          = Input.GetMouseButtonDown(0);
            characterInputs.ToggleTargetingMode = Input.GetKeyDown(KeyCode.Space);

            // Apply inputs to character
            Character.SetInputs(ref characterInputs);
        }
        /// <summary>
        /// This is called every frame by [NatePlayer] in order to tell the character what its inputs are
        /// </summary>
        public void SetInputs(ref PlayerCharacterInputs inputs)
        {
            // Clamp input (in this case we just do it by 1f)
            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);

            //Inputs while grounded
            if (Motor.GroundingStatus.IsStableOnGround)
            {
                if (inputs.SwordSwing)                      //Temp
                {
                    swordCoroutineScript.StartSwordSwing(); //Temporary, to start coroutine for canSwing check.
                }

                if (inputs.ToggleTargetingMode) //Temp target mode activation
                {
                    if (!camera.TargetingModeActive)
                    {
                        camera.TargetingModeActive = true;
                    }
                    else
                    {
                        camera.TargetingModeActive = false;
                    }
                }
            }
            else
            {
                camera.TargetingModeActive = false;
            }

            switch (CurrentCharacterState)
            {
            //Default inputs
            case CharacterState.Default:
            {
                _moveInputVector        = cameraPlanarRotation * moveInputVector;
                StableMovementSharpness = 7f;

                switch (OrientationMethod)
                {
                case OrientationMethod.TowardsMovement:
                    _lookInputVector = _moveInputVector.normalized;
                    break;
                }

                //Transition to crouching
                if (inputs.CrouchDown && Motor.GroundingStatus.IsStableOnGround)
                {
                    _shouldBeCrouching = true;

                    if (!_isCrouching)
                    {
                        _isCrouching = true;
                        Motor.SetCapsuleDimensions(0.5f, 1f, 0.5f);      //Scales the hitbox.
                        MeshRoot.localScale = new Vector3(1f, 0.5f, 1f); //Scales the mesh root.
                        TransitionToState(CharacterState.Crouched);
                    }
                }

                break;
            }

            //Crouched inputs
            case CharacterState.Crouched:
            {
                _moveInputVector        = cameraPlanarRotation * moveInputVector;
                StableMovementSharpness = 7f;

                switch (OrientationMethod)
                {
                case OrientationMethod.TowardsMovement:
                    _lookInputVector = _moveInputVector.normalized;
                    break;
                }

                // Stop crouching
                if (inputs.CrouchUp)
                {
                    _shouldBeCrouching = false;
                }

                break;
            }

            //Sword attack inputs
            case CharacterState.SwordAttack:     //Temp
            {
                _moveInputVector = Vector3.ClampMagnitude(Motor.CharacterForward, 1f);

                switch (CurrentSwordAttackState)
                {
                case SwordAttackState.SwingStart:
                {
                    StableMovementSharpness = 10f;
                    break;
                }

                case SwordAttackState.SwingEnd:
                {
                    StableMovementSharpness = 10f;
                    break;
                }
                }
                break;
            }
            }
        }
        /// <summary>
        /// This is called every frame by [NatePlayer] in order to tell the character what its inputs are
        /// </summary>
        public void SetInputs(ref PlayerCharacterInputs inputs)
        {
            // Clamp input (in this case we just do it by 1f)
            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);

            switch (CurrentNPCState)
            {
            case NPCState.Default:
            case NPCState.Crouched:
            {
                //canSwing = true; //Temp
                // Move and look inputs
                _moveInputVector        = cameraPlanarRotation * moveInputVector;
                StableMovementSharpness = 7f;

                // Crouching input
                if (inputs.CrouchDown)
                {
                    _shouldBeCrouching = true;

                    if (!_isCrouching)
                    {
                        _isCrouching = true;
                        Motor.SetCapsuleDimensions(0.5f, 1f, 0.5f);      //Scales the hitbox.
                        MeshRoot.localScale = new Vector3(1f, 0.5f, 1f); //Scales the mesh root.

                        if (CurrentNPCState == NPCState.Default)
                        {
                            TransitionToState(NPCState.Crouched);
                            //Transition to crouch
                        }
                    }
                }
                else if (inputs.CrouchUp)
                {
                    _shouldBeCrouching = false;
                }

                break;
            }

            case NPCState.SwordAttack:     //Temp
            {
                _moveInputVector = Vector3.ClampMagnitude(Motor.CharacterForward, 1f);
                break;
            }
            }
        }