Search Unity

Minor trouble With First Person Sneaking (Stealth game)

Discussion in 'Scripting' started by Banditmayonnaise, Jun 27, 2019.

  1. Banditmayonnaise

    Banditmayonnaise

    Joined:
    Apr 3, 2019
    Posts:
    10
    Hi
    I'm experimenting with some First Person movement mechanics. Right now I have trouble with my crouching/Sneaking movement...Everytime i crouch it works fine, but when i release the crouch button, the player kinda jumps up. I want know how to make it more smooth? I have tried different things to fix it, but i can't figure it out.I also want to change the movement speed when i crouch, but that can't wait for later. Now i just want to fix my crouching bug...

    Code (CSharp):
    1. //Private Variables
    2. [Header("Player Crouch")]
    3.     [SerializeField]
    4.     private KeyCode crouchKey;
    5.    /* [SerializeField]
    6.     private float _crouchSpeed;
    7.     [SerializeField]
    8.     private float _crouchHeight = 1.2f;*/
    9.  
    10. private void CrouchMethod()
    11.     {
    12.         if(Input.GetKey(crouchKey))
    13.         {
    14.           if(_controller.isGrounded)
    15.             {
    16.                 _controller.height = 1.2f;
    17.             }
    18.         }
    19.         else
    20.         {
    21.             _controller.height = 2.0f;
    22.         }
    23.     }
     

    Attached Files:

  2. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    in addition to setting height, try setting new center
     
    Banditmayonnaise likes this.
  3. Banditmayonnaise

    Banditmayonnaise

    Joined:
    Apr 3, 2019
    Posts:
    10
    Thanks, but how would someone do that?
     
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    This could be useful
    _controller.center = _controller.height / 2;

    Just do that after you set the height each time.
     
    Banditmayonnaise likes this.
  5. Banditmayonnaise

    Banditmayonnaise

    Joined:
    Apr 3, 2019
    Posts:
    10
    I tried that, but it shows this error "Cannot implicitly convert type 'float' to 'UnityEngine.Vector3'"
    I probably just don't get it, i'm pretty new at scripting. I could really use a explanation on how setting a center would help? (If you have the time, ofc)
    I also have done some reading and i thought about using Lerp, or Smoothdamp. So whenever player standup from crouching it will reach original height over time.
     
  6. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Oh, I didn't realize that center was a vector. Whoops. Try this instead
    _controller.center = Vector3.up * (_controller.height / 2);
     
    Banditmayonnaise likes this.
  7. charlesfelix442

    charlesfelix442

    Joined:
    May 25, 2019
    Posts:
    9
    Nice, clean and helpful.