Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

FREE simple FPC Move, Sprint, and Stamina system

Discussion in 'Scripting' started by Nightmare Games, Jun 17, 2013.

?

Was this usefull?

  1. Yes

    1 vote(s)
    50.0%
  2. No

    1 vote(s)
    50.0%
  1. Nightmare Games

    Nightmare Games

    Joined:
    Sep 5, 2012
    Posts:
    201
    Here is a simple charecter movement script with sprint and stamina, take note it is setup so you can have stamina potions and add difficulty to the game

    if you want to know how to set up this script check out the video at the bottom of this post

    Code (csharp):
    1. /* Charecter Movement by OMA at armedunity.com
    2. Stamina and sprint system by Mark Benitez "NG3D Tutorials" / "Nightmare Games" (HANDLES of Mark Benitez)
    3. Subscribe to my Youtube channel for more free scripts and assets: http://www.youtube.com/ng3d */
    4. var controller : CharacterController;
    5. private var moveDirection = Vector3.zero;
    6. private var limitDiagonalSpeed : boolean = true;
    7. private var myTransform : Transform;
    8. //Sprint and Stamina vars
    9. var walkSpeed : float = 100;
    10. var sprintSpeed : float = 300;
    11. var stamina : float = 5;
    12. var FPC : Transform;
    13. var Cam : Transform;
    14. var staminaDrain = false;
    15.  
    16. function Start ()
    17. {
    18.     myTransform = transform;
    19.     //sets the staminaDrain to false( we do not lose stamina)
    20.     staminaDrain = false;
    21. }
    22.  
    23. function Update ()
    24. {
    25.     if(Input.GetKey(KeyCode.LeftShift)  stamina > 1)
    26.     {
    27.     /*In order: 1.Waits for 2 seconds
    28.                 2. sets our walkSpeed to sprintSpeed so we can sprint
    29.                 3. sets staminaDrain to true
    30.                 4.Calls the staminaDecrease function*/
    31.         WaitForSeconds(2);
    32.         walkSpeed = sprintSpeed;
    33.         staminaDrain=true;
    34.         staminaDecrease();
    35.     }
    36.     else
    37.     {
    38.     /*In order: 1.Walk speed sets to 100 (normal speed)
    39.                 2. sets staminaDrain to true
    40.                 3. Call the staminaDecrease function*/
    41.         walkSpeed = 100;
    42.         staminaDrain=false;
    43.         staminaDecrease();
    44.     }
    45.     /*  makes sure so our camera always has the same position as our FPC and
    46.         makes sure our FPC rotates the same as our camera so we walk correctly*/
    47.     Cam.transform.position = FPC.position;
    48.     FPC.transform.rotation = Cam.rotation;
    49.     var inputX = Input.GetAxis("Horizontal");
    50.     var inputY = Input.GetAxis("Vertical");
    51.     var inputModifyFactor = (inputX != 0.0  inputY != 0.0  limitDiagonalSpeed)? .7071 : 1.0;
    52.    
    53.     moveDirection = Vector3(inputX * inputModifyFactor, 0, inputY * inputModifyFactor);
    54.         moveDirection = myTransform.TransformDirection(moveDirection);
    55.     moveDirection *= walkSpeed;
    56.    
    57.     controller.SimpleMove(moveDirection * Time.deltaTime);
    58. }
    59.  
    60. function staminaDecrease ()
    61. {
    62.     if(staminaDrain == true)
    63.     {   //subtracts our stamina by 1 per second
    64.         stamina-=1 * Time.deltaTime;
    65.         // calls the function numberProblems
    66.         numberProblems ();
    67.     }
    68.     else if(staminaDrain == false)
    69.     {   //subtracts our stamina by 1 per second
    70.         stamina+=1 * Time.deltaTime;
    71.         // calls the function numberProblems
    72.         numberProblems ();
    73.     }
    74. }
    75. function numberProblems ()
    76. {
    77.     if(stamina < 1)
    78.     {   //sets stamina to 0, therefore we need a stamina potion in order to sprint again
    79.         stamina = 0;
    80.         // sets our walk speed to 0
    81.         walkSpeed = 100;               
    82.     }
    83.     else if(stamina > 5)
    84.     {   // sets our stamina to full value
    85.         stamina = 5;
    86.     }
    87. }