Search Unity

HeadBob and Crouching First Person

Discussion in 'Scripting' started by Banditmayonnaise, Jul 1, 2019.

  1. Banditmayonnaise

    Banditmayonnaise

    Joined:
    Apr 3, 2019
    Posts:
    10
    Hi
    I have now created a functional smooth crouching system. There are some small issues but i will try to fix them later, for now the biggest problem is that my crouching script interfere with my headbobbing script. So i can't have them both on at the same time...
    So i would like to know what i should do to have them both. Maybe there is another method to create headbobbing. I know animation is also a thing when creating headbobing, but i dont know anything about animation, so I prefer scripting.

    Code (CSharp):
    1. #region Crouch Bug City
    2.     private void CrouchFunction()
    3.     {
    4.         previous_Y = _controller.transform.position.y - _controller.height / 2 - _controller.skinWidth;
    5.         if (Input.GetKeyDown(crouchKey))
    6.             if (_controller.isGrounded)
    7.             {
    8.                 if (_isCrouch == false)
    9.                 {
    10.                     _isCrouch = true;
    11.                     _targetHeight = _crouchHeight;
    12.                 }
    13.                 else
    14.                 {
    15.                     _isCrouch = false;
    16.                     _targetHeight = _standingHeight;
    17.                 }
    18.             }
    19.  
    20.         _controller.height = Mathf.Lerp(_controller.height, _targetHeight, 5f * Time.deltaTime);
    21.         if(_controllerCamera = null)
    22.  
    23.         _controllerCamera.transform.position = Vector3.Lerp(_controllerCamera.transform.position, new Vector3(_controllerCamera.transform.position.x, _controllerCamera.transform.position.y + _targetHeight / 2 - 0.1f, _controllerCamera.transform.position.z), 5.0f * Time.deltaTime);
    24.         //^^The Problem^^
    25.  
    26.         _controller.transform.position = Vector3.Lerp(_controller.transform.position, new Vector3(_controller.transform.position.x, previous_Y + _targetHeight / 2 + _controller.skinWidth, _controller.transform.position.z), 5.0f * Time.deltaTime);
    27.     }
    28.     #endregion
    And now to the Headbobbing script.

    Code (CSharp):
    1. /* #region Head boobing/Swaying
    2.     private void HeadBobbing()
    3.     {
    4.         float waveslice = 0.0f;
    5.         float horizontal = Input.GetAxis("Horizontal");
    6.         float vertical = Input.GetAxis("Vertical");
    7.  
    8.         Vector3 cSharpConversion = transform.localPosition;
    9.  
    10.         if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0)
    11.         {
    12.             timer = 0.0f;
    13.         }
    14.         else
    15.         {
    16.             waveslice = Mathf.Sin(timer);
    17.             timer = timer + _bobSpeed;
    18.             if ( timer > Mathf.PI * 2)
    19.             {
    20.                 timer = timer - (Mathf.PI * 2);
    21.             }
    22.         }
    23.         if(waveslice != 0)
    24.         {
    25.             float translateChange = waveslice * _bobAmount;
    26.             float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
    27.             totalAxes = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
    28.             translateChange = totalAxes * translateChange;
    29.             cSharpConversion.y = _midPoint + translateChange;
    30.         }
    31.         else
    32.         {
    33.             cSharpConversion.y = _midPoint;
    34.         }
    35.  
    36.         transform.localPosition = cSharpConversion;
    37.     }
    38.     #endregion*/
    Any tips, feedback and comments will be much appreciated.