示例#1
0
        /// <summary>
        /// This is called every frame by Player in order to tell the character what its inputs are.
        /// </summary>
        public void SetInputs(ref PlayerCharacterInputs playerInputs)
        {
            Vector3 _moveInputVector = Vector3.ClampMagnitude(new Vector3(playerInputs.MoveAxisRight, 0f, playerInputs.MoveAxisForward), 1f);

            float lookInput = 0;

            if (playerInputs.RotationMethod == RotationMethod.KeyboardRotation)
            {
                lookInput = Motor.transform.rotation.y + playerInputs.MoveAxisRight * 25f; //TODO: Replace hardcoding with proper sensitivity value
            }
            else if (playerInputs.RotationMethod == RotationMethod.MouseJoystick)
            {
                Vector3 mousePosition = new Vector3(Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.height, 0);
                Vector3 screenCenter  = new Vector3(0.5f, 0.5f, 0);

                lookInput = Mathf.Atan2(mousePosition.y - screenCenter.y, mousePosition.x - screenCenter.x) * (180f / Mathf.PI);
            }

            #region Camera-Rotation

            Vector3 _cameraPlanarDirection = Vector3.ProjectOnPlane(playerInputs.CameraRotation * Vector3.forward, Motor.CharacterUp).normalized;
            if (_cameraPlanarDirection.sqrMagnitude == 0f)
            {
                _cameraPlanarDirection = Vector3.ProjectOnPlane(playerInputs.CameraRotation * Vector3.up, Motor.CharacterUp).normalized;
            }
            Quaternion cameraPlanarRotation = Quaternion.LookRotation(_cameraPlanarDirection, Motor.CharacterUp);

            #endregion

            #region Character-Rotation

            //LookInputVector = new Vector3(lookInput, 0, 0f);

            Quaternion rotationFromInput         = Quaternion.Euler(Motor.CharacterUp * lookInput);
            Vector3    _characterPlanarDirection = Vector3.Cross(Motor.CharacterUp,
                                                                 Vector3.Cross((rotationFromInput * Motor.CharacterForward),
                                                                               Motor.CharacterUp));
            Quaternion characterPlanarRotation = Quaternion.LookRotation(_characterPlanarDirection, Motor.CharacterUp);

            /*
             * Quaternion rotationFromInput = Quaternion.Euler(FollowTransform.up * (rotationInput.x * RotationSpeed));
             * PlanarDirection = rotationFromInput * PlanarDirection;
             * PlanarDirection = Vector3.Cross(FollowTransform.up, Vector3.Cross(PlanarDirection, FollowTransform.up));
             * Quaternion planarRot = Quaternion.LookRotation(PlanarDirection, FollowTransform.up);
             */


            #endregion

            //LookInputVector = new Vector3(lookInput, 0, 0f);

            switch (playerInputs.RotationMethod)
            {
            case (RotationMethod.NoRotation):
            {
                LookInputVector = _cameraPlanarDirection;
                break;
            }

            case (RotationMethod.KeyboardRotation):
            {
                //_moveInputVector = new Vector3(0, _moveInputVector.y, _moveInputVector.z);
                LookInputVector = _characterPlanarDirection;
                break;
            }

            case (RotationMethod.VelocityRotation):
            {
                LookInputVector = Motor.BaseVelocity;
                break;
            }

            case (RotationMethod.MouseJoystick):
            {
                //_moveInputVector = new Vector3(0, _moveInputVector.y, _moveInputVector.z);
                LookInputVector = _characterPlanarDirection;
                break;
            }
            }

            switch (playerInputs.MovementMethod)
            {
            case (MovementMethod.StaticForward):
            {
                MoveInputVector = _moveInputVector;
                break;
            }

            case (MovementMethod.CameraIsForward):
            {
                MoveInputVector = cameraPlanarRotation * _moveInputVector;
                break;
            }

            case (MovementMethod.CharacterForwardIsForward):
            {
                MoveInputVector = characterPlanarRotation * _moveInputVector;
                break;
            }

            default:
            {
                MoveInputVector = _moveInputVector;
                break;
            }
            }

            MethodOfMovement = playerInputs.MovementMethod;
            MethodOfRotation = playerInputs.RotationMethod;
        }