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

Sprinting Script

Discussion in 'Scripting' started by cjfarmar100, Aug 5, 2011.

  1. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
    I was wondering if someone could help me with a sprinting script that lets you sprint with key shift, and has a 6 second running time and a 5 second recharge time. 24 speed should do it.

    Best Regards,
    -cjfarmar100
     
  2. Mr.Smart

    Mr.Smart

    Joined:
    Aug 5, 2011
    Posts:
    54
    you must add var RunTime , var RechargTime, after if(Input.GetKeyDown(KeyCode.LeftShift) runningKeyenable =true) decreaseruntime = true,
    after in update use
    if(decreaseruntime){ start to decrement it by one
    if(RunTime == 0){ runing =false}
    if (running == flase){ RechargTime--;
    if(RechargTime == 0){ runningKeyenable =true }

    try to do similare to this
     
  3. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
    Thank for the help!
     
  4. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
    ok this is what i got so far, but, i get many errors, do you think you could help me fix these?
    var runtime = 6.0f;
    var rechargetime = 5.0f;
    if(Input.GetKeyDown(KeyCode.LeftShift)(runningKeyenable) = True)
    decreaseruntime = True)
    function Update () {
    if(decreaseruntime)(start decrement it by one)
    if (Runtime == 0)(running =False);
    if(running == False(RechargeTime05;)
    if(RechargeTime == 0)running keyenable = True;
    }
     
  5. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    i'll do a script for you, wait a second, and use code tags in posts :)

    ================edit============
    phew... i am very tired, did something, but i didn't test it, see if it works:
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Sprint : MonoBehaviour {
    7.     private float boosterValue;
    8.     private float coolDownTimer;
    9.     private float durationTimer;
    10.     private bool sprinting;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         sprinting = false;
    15.         boosterValue = 24.0f;
    16.         coolDownTimer = 0;
    17.         durationTimer = 0;
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         if(Input.GetKey(KeyCode.UpArrow)){//if we press up arrow
    23.             if(sprinting){
    24.                 if(Input.GetKeyDown(KeyCode.LeftShift)  coolDownTimer <= 0){
    25.                     durationTimer = 6.0f;
    26.                     sprinting = true;
    27.                 }
    28.                 durationTimer -= Time.deltaTime;
    29.                 if(durationTimer >= 0  Input.GetKeyDown(KeyCode.LeftShift)){
    30.                     //apply speed here, for example transform.position = transform.forward * boosterValue * Time.deltatime (you figure it out how you want it to be)
    31.                 }
    32.                 else{
    33.                     //apply normal speed here
    34.                 }
    35.                
    36.                 if(durationTimer <= 0  sprinting){
    37.                     coolDownTimer = 5.0f;
    38.                     sprinting = false;
    39.                 }
    40.                
    41.                 coolDownTimer -= Time.deltaTime;
    42.                 if(coolDownTimer <= 0){
    43.                     coolDownTimer = 0;
    44.                 }
    45.             }
    46.             else{
    47.                 //apply normal speed here
    48.             }
    49.            
    50.         }
    51.    
    52.     }
    53. }
    54.  
     
    Last edited: Aug 5, 2011
  6. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
    Thank ya, this will help me alot, but it says, cant script behavior running, because the script file name does not match the class defined in the script, do you know what i should do?
     
    Last edited: Aug 5, 2011
  7. xxxDjdogxxx

    xxxDjdogxxx

    Joined:
    Mar 28, 2011
    Posts:
    751
    hmmmmmm, you may have a prob here.
     
  8. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    you have to name your C# script "Sprint" without the commas.
     
  9. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
    Ohhh, haha sorry about that, now that i got it on there though, it will not let me change the value's of the boost, what should i do then?
     
  10. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    in the code, here:
    Code (csharp):
    1.  
    2.  
    3. void Start () {
    4.         sprinting = false;
    5.         boosterValue = 24.0f;
    6.         coolDownTimer = 0;
    7.         durationTimer = 0;
    8.     }
    9.  
    change the boosterValue to whatever you feel like it.
     
  11. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
    ok now i cant move, even though i changed all of that, do i need another script for controls, cause i already made one?
     
  12. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    i think i got it wrong here
    Code (csharp):
    1. sprinting = false;
    make it
    Code (csharp):
    1. sprinting = true;
    i am very tired... it is very hard for me to concentrate now.. have fun with the script. i'm off to bed!
     
  13. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
    oh ok, well it didnt work, umm when you wake up, i guess you can try, ill talk to ya soon
     
  14. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
  15. cjfarmar100

    cjfarmar100

    Joined:
    Aug 5, 2011
    Posts:
    33
  16. Eiznek

    Eiznek

    Joined:
    Jun 9, 2011
    Posts:
    374
    You just simply need to apply a character controller/Character Motor.. And then you can alter the move data with your custom script. Its going to be very specific to whichever movement script you use. Honestly this is very basic as well you should not be asking others to write it for you, Reading the documentation and learning how to do it yourself would be far more beneficial.

    Code (csharp):
    1. public CharacterMotor motorRef; //Drag and Drop your Character Motor Reference into the Inspector.
    Now you can call motorRef.speed or whatever it might be and update it based on your custom script.
     
  17. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
  18. Lonnie211

    Lonnie211

    Joined:
    Jul 6, 2013
    Posts:
    4
    ^ thanks for the script ar0nax !!
     
  19. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    you're welcome! :)