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

Sprint Script?

Discussion in 'Scripting' started by Treasureman, Aug 6, 2014.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I'm making a first person game and I only need a sprint script so that when I hold in Left or Right Shift then you'll move faster and the camera will move back slightly. I don't need any animations, just the movement.

    Here's what I have so far:

    var regSpeed: float = 3.5;
    var sprintSpeed: float = 7.0;

    function Update(){
    var speed = regSpeed;
    if (Input.GetKey("left shift") || Input.GetKey("right shift")){
    speed = sprintSpeed;
    }
    ...
    character.Move(moveDir * speed * Time.deltaTime);
    }

    But it won't work. Thanks if you can help!
     
  2. DarkArts-Studios

    DarkArts-Studios

    Joined:
    May 2, 2013
    Posts:
    389
    First, have a look at using code tags (for next time).

    Then try:

    Code (JavaScript):
    1.  
    2. var regSpeed: float = 3.5;
    3. var sprintSpeed: float = 7.0;
    4.  
    5. function Update(){
    6.     var speed = regSpeed;
    7.  
    8.     if ( Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) )
    9.     {
    10.         speed = sprintSpeed;
    11.     }
    12.  
    13.     //...
    14.  
    15.     character.Move(moveDir * speed * Time.deltaTime);
    16. }
    17.  
    Reference: KeyCode
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I tried it but i got 2 compiler errors?!
    I tried it and got 2 compiler errors?!