I can't get sprint working (C#),I can't get sprinting to work

Im new and tried to use a script i found online but i think it collides with my current script help pls.

Sprint script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sprint : MonoBehaviour
{
public float walkSpeed;
public float sprintSpeed;

// Start is called before the first frame update
void Start()
{
    walkSpeed = 10f;
    sprintSpeed = 30f;
}

// Update is called once per frame
void FixedUpdate()
{
    transform.Translate(walkSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, walkSpeed * Input.GetAxis("Vertical") * Time.deltaTime);

    if (Input.GetKey(KeyCode.LeftShift))
    {
        walkSpeed = sprintSpeed;
    }
    else
    {
        walkSpeed = walkSpeed;
    }

    if(Input.GetKey(KeyCode.LeftShift))
    {
        walkSpeed = 10f;
    }
}

}

My movement script
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
Vector3 velocity;
public float gravity = -9.81f;
public float groundDistance = 0.4f;
public Transform GroundCheck;
public LayerMask groundMask;
bool isGrounded;
public float jumpHeight = 3f;

// Update is called once per frame
void Update()
{
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    Vector3 move = transform.right * x + transform.forward * z;
    controller.Move(move * speed * Time.deltaTime);
    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
    isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask);
    if(isGrounded && velocity.y <0)
    {
        velocity.y = -2f;
    }

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

}

}

,

Hi!

Well, if you already have implemented move script then you don’t want to put another one on top of it - you only want to expand yours based on the one you found.


So, essentially, the sprint is just higher speed variable, so the way you can implement it in your code could look like this:

   public CharacterController controller;

    float f_ActualSpeed = 12f;                // Actual speed, based on Input
    public float speed = 12f;
    public float runSpeed = 24f;        // The speed when L.Shift pressed

    Vector3 velocity;
    public float gravity = -9.81f;
    public float groundDistance = 0.4f;
    public Transform GroundCheck;
    public LayerMask groundMask;
    bool isGrounded;
    public float jumpHeight = 3f;

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        /* If we pressed LeftShift down we will assign f_ActualSpeed to our runSpeed
         * If we released LeftShift up then we assign it back to speed variable */
        if (Input.GetKeyDown(KeyCode.LeftShift))
            f_ActualSpeed = runSpeed;
        else if (Input.GetKeyUp(KeyCode.LeftShift))
            f_ActualSpeed = speed;

        // Changed speed variable to f_ActualSpeed
        controller.Move(move * f_ActualSpeed * Time.deltaTime);

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);

        isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask);
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
    }

I’ve commented out the part that I added or adjusted, but it’s only couple of lines.


The f_ActualSpeed variable it is our adjusting speed that we will adjust based on Input and then multiply for .Move calculations;

The speed variable it is base speed / walk speed;

The runSpeed variable it is our run speed.

By pressing Left Shift we assing f_ActualSpeed to our runSpeed so the next time .Move will be calculated we wil move faster;

By releasing Left Shift we assign f_ActualSpeed back to the speed variable.


Hope it helps.