Search Unity

Need Help Creating A "sprint" Script.

Discussion in 'Scripting' started by MikawasakiDigital, Apr 14, 2019.

  1. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Hey guys, thanks for taking the time to offer any insight you may have.

    I'm new to Coding and could really use the help.

    I'm creating a First Person Perspective game, and really need help with setting up a script allowing a player to "Sprint".

    If theirs anyway this could be done on a separate script please elaborate how.

    Here's what I currently have.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(PlayerMotor))]
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     private float speed = 5f;
    10.  
    11.     [SerializeField]
    12.     private float lookSensitivity = 3f;
    13.  
    14.     private PlayerMotor motor;
    15.  
    16.     void Start ()
    17.     {
    18.         motor = GetComponent<PlayerMotor>();
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.         float xMov = Input.GetAxisRaw("Horizontal");
    24.  
    25.         float zMov = Input.GetAxisRaw("Vertical");
    26.  
    27.         Vector3 movHorizontal = transform.right * xMov;
    28.  
    29.         Vector3 movVertical = transform.forward * zMov;
    30.  
    31.         Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
    32.  
    33.         motor.Move (velocity);
    34.  
    35.         float yRot = Input.GetAxisRaw("Mouse X");
    36.  
    37.         Vector3 rotation = new Vector3(0f, yRot, 0f) * lookSensitivity;
    38.  
    39.         motor.Rotate(rotation);
    40.  
    41.         float xRot = Input.GetAxisRaw("Mouse Y");
    42.  
    43.         Vector3 cameraRotation = new Vector3(xRot, 0f, 0f) * lookSensitivity;
    44.  
    45.         motor.RotateCamera(cameraRotation);
    46.     }
    47. }
    48.  
     
  2. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    id try this
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         speed = 5;
    4.         if (Input.GetKey(KeyCode.LeftShift) {
    5.             speed = 10;
    6.         }
    7.         float xMov = Input.GetAxisRaw("Horizontal");
    8.  
    9.         float zMov = Input.GetAxisRaw("Vertical");
    10.  
    11.         Vector3 movHorizontal = transform.right * xMov;
    12.  
    13.         Vector3 movVertical = transform.forward * zMov;
    14.  
    15.         Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
    16.  
    17.         motor.Move (velocity);
    18.  
    19.         float yRot = Input.GetAxisRaw("Mouse X");
    20.  
    21.         Vector3 rotation = new Vector3(0f, yRot, 0f) * lookSensitivity;
    22.  
    23.         motor.Rotate(rotation);
    24.  
    25.         float xRot = Input.GetAxisRaw("Mouse Y");
    26.  
    27.         Vector3 cameraRotation = new Vector3(xRot, 0f, 0f) * lookSensitivity;
    28.  
    29.         motor.RotateCamera(cameraRotation);
    30.     }
     
    MikawasakiDigital likes this.
  3. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Thank you so much! That makes perfect sense, thanks for this.