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

Help with a while loop crash

Discussion in 'Scripting' started by Jiggly_puff, Apr 29, 2020.

  1. Jiggly_puff

    Jiggly_puff

    Joined:
    Mar 13, 2013
    Posts:
    2
    Hi Guys,

    I'm quite new to coding and am trying to figure out a crash I have implemented in my while loop, see below.

    Once shift is pressed, unity crashes. I'm just changing the value of the runSpeed float with SprintSpeed while the button is pressed then going back to default once the button is no longer pressed.

    Cheers for the help!


    while (Input.GetKeyDown(KeyCode.LeftShift))
    {
    runSpeed = sprintSpeed;
    if (!Input.GetKeyDown(KeyCode.LeftShift))
    {
    break;
    }
    }




    Below is the entire script for reference.

    public class PlayerMovement : MonoBehaviour
    {

    public CharacterController controller;

    public float runSpeed = 12f;
    public float sprintSpeed = 30f;
    public float crouchSpeed = 8f;
    public float jumpHeight = 3f;
    public float gravity = -9.81f;

    public Transform groundCheck;
    public LayerMask groundMask;

    private float groundDistance = 0.4f;

    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {

    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

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

    controller.Move(move * runSpeed * Time.deltaTime);

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

    while (Input.GetKeyDown(KeyCode.LeftShift))
    {
    runSpeed = sprintSpeed;
    if (!Input.GetKeyDown(KeyCode.LeftShift))
    {
    break;
    }
    }

    //while (Input.GetKeyDown(KeyCode.LeftControl))
    //{
    // runSpeed = crouchSpeed;
    //}

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
    }
    }
     
    Sam23Ab likes this.
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Change
    Code (CSharp):
    1. while (Input.GetKeyDown(KeyCode.LeftShift))
    to
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.LeftShift))
     
    Sam23Ab likes this.
  3. Jiggly_puff

    Jiggly_puff

    Joined:
    Mar 13, 2013
    Posts:
    2
    Thanks for the reply, I used to have it that way but I want the value to return to default after the button is no longer pressed, I figured a while loop would be more efficient than an if and else statement.
     
  4. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    In addition to what I previously suggested add
    Code (CSharp):
    1. if (Input.GetKeyUp(KeyCode.LeftShift))
    2.     speed = normalSpeed;
    To your update function.