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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Sprint Functionality Not Working

Discussion in 'Scripting' started by larrypickle, Jun 1, 2020.

  1. larrypickle

    larrypickle

    Joined:
    Oct 8, 2019
    Posts:
    8
    I'm trying to get my player to run when I press the left shift down, but no matter how I try and modify the code, it only ever moves according to walkSpeed and never to sprintSpeed even though I know that the velocity is being set to sprintSpeed when I press left shift down. Any help would be appreciated.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterController : MonoBehaviour
    6. {
    7.     //8 direction character controller
    8.     //https://www.youtube.com/watch?v=cVy-NTjqZR8
    9.  
    10.     float velocity = 5;
    11.     public float turnSpeed;
    12.     public float walkSpeed;
    13.     public float sprintSpeed;
    14.     public bool isSprinting;
    15.  
    16.     Animator anim;
    17.  
    18.     Vector2 playerInput;
    19.     float angle; //to know how to rotate player
    20.  
    21.     Quaternion targetRotation; //quaternion used to represent rotations
    22.     Transform cam; // to keep track of camera angles
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         cam = Camera.main.transform; //this accesses the main camera
    28.         anim = GetComponent<Animator>();
    29.  
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         GetInput();//get input every frame, so the thing stops when
    36.         if (Mathf.Abs(playerInput.x) < 1 && Mathf.Abs(playerInput.y) < 1)
    37.         { //this stops the character from moving when no input
    38.             anim.SetInteger("condition", 0);
    39.  
    40.             return;
    41.         }
    42.  
    43.         else
    44.         {
    45.             anim.SetInteger("condition", 1);
    46.         }
    47.  
    48.         CalculateDirection();
    49.         Rotate();
    50.         if (Input.GetKeyDown(KeyCode.LeftShift))
    51.         {
    52.             isSprinting = true;
    53.         }
    54.  
    55.         else
    56.         {
    57.             isSprinting = false;
    58.         }
    59.         Move();
    60.  
    61.  
    62.     }
    63.  
    64.  
    65.     void GetInput()
    66.     {
    67.         playerInput.x = Input.GetAxisRaw("Horizontal");
    68.         playerInput.y = Input.GetAxisRaw("Vertical");
    69.     }
    70.  
    71.     void CalculateDirection()
    72.     {
    73.         angle = Mathf.Atan2(playerInput.x, playerInput.y); //give us radians, tangent between up and horizontal
    74.         angle = Mathf.Rad2Deg * angle; // to convert to degrees
    75.         angle += cam.eulerAngles.y;
    76.     }
    77.  
    78.     void Rotate()
    79.     {
    80.         targetRotation = Quaternion.Euler(0, angle, 0); // convert euler angles to quaternions
    81.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
    82.     }
    83.  
    84.     void Move()
    85.     {
    86.         if(isSprinting == true)
    87.         {
    88.             velocity = sprintSpeed;
    89.             Debug.Log("debug: " + sprintSpeed + "velocity: " + velocity);
    90.  
    91.         }
    92.  
    93.         else
    94.         {
    95.             velocity = walkSpeed;
    96.         }
    97.  
    98.         Debug.Log("debug velocity: " + velocity);
    99.  
    100.         anim.SetFloat("velocity", velocity / 10);
    101.  
    102.         transform.position += transform.forward * walkSpeed * Time.deltaTime;
    103.  
    104.     }
    105.  
    106.  
    107. }
    108.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    larrypickle likes this.
  3. larrypickle

    larrypickle

    Joined:
    Oct 8, 2019
    Posts:
    8
    Thanks so much that worked perfectly!
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I also noticed on line 102 you are always using walkSpeed:
    Code (CSharp):
    1. transform.position += transform.forward * walkSpeed * Time.deltaTime;
    Did you mean this:
    Code (CSharp):
    1. transform.position += transform.forward * velocity * Time.deltaTime;