Need Help Creating A "sprint" Script.

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.

using UnityEngine;

[RequireComponent(typeof(PlayerMotor))]

public class PlayerController : MonoBehaviour
{

    [SerializeField]
    private float speed = 5f;

    [SerializeField]
    private float lookSensitivity = 3f;

    private PlayerMotor motor;

    void Start ()
    {
        motor = GetComponent<PlayerMotor>();
    }

    void Update ()
    {
        float xMov = Input.GetAxisRaw("Horizontal");

        float zMov = Input.GetAxisRaw("Vertical");

        Vector3 movHorizontal = transform.right * xMov;

        Vector3 movVertical = transform.forward * zMov;

        Vector3 velocity = (movHorizontal + movVertical).normalized * speed;

        motor.Move (velocity);

        float yRot = Input.GetAxisRaw("Mouse X");

        Vector3 rotation = new Vector3(0f, yRot, 0f) * lookSensitivity;

        motor.Rotate(rotation);

        float xRot = Input.GetAxisRaw("Mouse Y");

        Vector3 cameraRotation = new Vector3(xRot, 0f, 0f) * lookSensitivity;

        motor.RotateCamera(cameraRotation);
    }
}

id try this

void Update ()
    {
        speed = 5;
        if (Input.GetKey(KeyCode.LeftShift) {
            speed = 10;
        }
        float xMov = Input.GetAxisRaw("Horizontal");

        float zMov = Input.GetAxisRaw("Vertical");

        Vector3 movHorizontal = transform.right * xMov;

        Vector3 movVertical = transform.forward * zMov;

        Vector3 velocity = (movHorizontal + movVertical).normalized * speed;

        motor.Move (velocity);

        float yRot = Input.GetAxisRaw("Mouse X");

        Vector3 rotation = new Vector3(0f, yRot, 0f) * lookSensitivity;

        motor.Rotate(rotation);

        float xRot = Input.GetAxisRaw("Mouse Y");

        Vector3 cameraRotation = new Vector3(xRot, 0f, 0f) * lookSensitivity;

        motor.RotateCamera(cameraRotation);
    }
1 Like

Thank you so much! That makes perfect sense, thanks for this.