Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Slide Mechanic in FPS help

Discussion in 'Scripting' started by joskuzera, Mar 29, 2021.

  1. joskuzera

    joskuzera

    Joined:
    Mar 8, 2021
    Posts:
    3
    So I managed to make a slide function in my FPS game on unity, the issue with it being the slide is locked to the position the user was looking when the slide was triggered.

    What I am looking to do is allow the slide to change directions based on the direction the user is looking as well as add some sort of impairment, meaning it shouldn't be as easy to move as if the user was just walking.

    ##Note I am Very New to Game Development##

    This is all the code used by my slide:
    Code (CSharp):
    1.     private void Slide()
    2.     {
    3.         if(isSprinting && !StanceCheckReturn(PlayerStance.Crouch) && !StanceCheckReturn(PlayerStance.Prone))
    4.         {
    5.             currentSlideSpeed = playerSettings.SlideSpeed;
    6.             slideDirection = transform.forward;
    7.             isSliding = true;
    8.  
    9.             if(playerStance == PlayerStance.Slide)
    10.             {
    11.                 if(StanceCheck(playerStandStance.StanceCollider.height))
    12.                 {
    13.                     return;
    14.                 }
    15.  
    16.                 playerStance = PlayerStance.Stand;
    17.                 return;
    18.             }
    19.  
    20.             if(StanceCheck(playerCrouchStance.StanceCollider.height))
    21.             {
    22.                 return;
    23.             }
    24.  
    25.             playerStance = PlayerStance.Slide;
    26.         }
    27.     }
    ##################################################################
    Code (CSharp):
    1.     void CalculateSlideTimer()
    2.     {
    3.         var crouchSpeed = playerSettings.WalkingForwardSpeed * playerSettings.CrouchSpeedEffector;
    4.         currentSlideSpeed -= playerSettings.SlideDuration * (input_Movement.y < 0.4f ? Time.deltaTime * 2 : Time.deltaTime);
    5.  
    6.         if (currentSlideSpeed <= (crouchSpeed) && playerStance == PlayerStance.Slide)
    7.         {
    8.             Debug.Log(crouchSpeed);
    9.             isSliding = false;
    10.             playerStance = PlayerStance.Crouch;
    11.         }
    12.     }
    ##################################################################
    This is in my calculate move function
    Code (CSharp):
    1.         if(isSliding == true)
    2.         {
    3.             var slideDirectionValue = slideDirection * currentSlideSpeed;
    4.             slideDirectionValue.y = movementSpeed.y;
    5.  
    6.             CalculateSlideTimer();
    7.             characterController.Move(slideDirectionValue * Time.deltaTime);
    8.  
    9.             return;
    10.         }
    11.         characterController.Move(movementSpeed);
    Any help would be great!
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Change the slide direction by athe look direction of your player multiplied by a factor and delta Time. Normalize the result so that the magnitude of slideDirection stays one (else your player would get faster).
    So inside your isSliding == true condition have something like
    slideDirection = (slideDirection + (myPlayer.forward * aFactorOfMyChoice * Time.deltaTime)).normalized;
     
  3. joskuzera

    joskuzera

    Joined:
    Mar 8, 2021
    Posts:
    3
    I don't really get what you mean here. I kind of do but I dont know how to get the look direction and then once I do get it where to put it. Nor do I know if you just want me to add slideDirection = (slideDirection + (myPlayer.forward * aFactorOfMyChoice * Time.deltaTime)).normalized; that line or remove other lines and replace with that?
     
  4. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    myPlayer.forward has to be replaced by the transform that represents your player.
    aFactorOfMyChoice should be a float variable to control the amount of influence you want the player view to have (you can simply write 0.25f or any other number here, if you want).
    You can use the provided line configured to your needs in line 3 of the last code snippet provided.