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

Need help with speed boost timer

Discussion in 'Scripting' started by S3Critsss, Jun 15, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I might get rid of this but for now ill keep it. i need help with making this timer for my speedboost work but when i run the game the speed boost starts automatically and it never ends even if the timers reach the lowest number i set.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControls : MonoBehaviour
    6. {
    7.  
    8.     public float moveSpeed = 5f;
    9.  
    10.     public bool isSpeeding = false;
    11.     public float TimeBetweenSpeedBoost = 10f;
    12.     public float TimeTillBoostEnds = 5f;
    13.     public float reducingTime = 0.2f;
    14.  
    15.     Rigidbody2D rb;
    16.  
    17.     private void Start()
    18.     {
    19.         rb = this.GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         //Player Faces Mouse
    25.         Vector3 mousePos = Input.mousePosition;
    26.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    27.         Vector2 direction = new Vector2(mousePos.x - transform.position.x,mousePos.y - transform.position.y);
    28.         transform.up = direction;
    29.  
    30.         //Player Moves Towards Mouse
    31.         direction.Normalize();
    32.         rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    33.  
    34.         //SpeedBoost
    35.         TimeBetweenSpeedBoost -= reducingTime * Time.deltaTime;
    36.         if (TimeBetweenSpeedBoost <= 0f);
    37.         {
    38.             isSpeeding = true;
    39.             if (isSpeeding)
    40.             {
    41.                 moveSpeed = 40f;
    42.                 TimeTillBoostEnds -= reducingTime * Time.deltaTime;
    43.                 if (TimeTillBoostEnds <= 0f)
    44.                 {
    45.                     isSpeeding = false;
    46.                     moveSpeed = 20f;
    47.                     TimeBetweenSpeedBoost = 10f;
    48.                     TimeTillBoostEnds = 5f;
    49.                 }
    50.             }
    51.         }
    52.     }
    53. }
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    There's a semicolon too much in line 36 .. if you remove it the script works.
     
  3. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,078
    Code looks OK. Check if reducingTime has a high value in the inspector.

    Edit:
    Ah, good catch. That semicolon already ends the if-statement and the code below is just a block which will be executed.
     
  4. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    Oh thank you i kept changing the script and i didn't even notice