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 Beginner problem Blink ability cooldown Help Needed

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

  1. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    I'm adding a tracer like blink ability and I've tried to add a cooldown to the ability but I can't figure it our, any help would be appreciated
    here's the code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Blink : MonoBehaviour
    6. {
    7.     public float BlinkRefreshTime = 5.0f;
    8.     public float distance = 5.0f;
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.  
    15.     void Update()
    16.     {
    17.         if (Input.GetAxis("Blinke")!=0)
    18.         {
    19.             BlinkForward();
    20.         }
    21.     }
    22.     public void BlinkForward()
    23.     {
    24.        
    25.         RaycastHit hit;
    26.         Vector3 destination = transform.position + transform.forward * distance;
    27.        
    28.         if (Physics.Linecast(transform.position, destination, out hit))
    29.         {
    30.             destination = transform.position + transform.forward * (hit.distance - 1f);
    31.            
    32.         }
    33.  
    34.         if (Physics.Raycast(destination, -Vector3.up, out hit))
    35.         {
    36.             destination = hit.point;
    37.             destination.y = 0.5f;
    38.             transform.position = destination;
    39.            
    40.         }
    41.        
    42.     }
    43. }
    44.  
    if you need anything else, please, just ask.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Code (CSharp):
    1. public float timeOfLastBlink;
    When blinking:
    Code (CSharp):
    1. timeOfLastBlink = Time.time;
    To check if you can blink again:
    Code (CSharp):
    1. if (Time.time > timeOfLastBlink + BlinkRefreshTime)
     
  3. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    so now using this code where can I change the amount of time it will take to have the ability cool down?
     
  4. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    Nevermind I'm so stupid, I just looked and figured it out
     
  5. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    thanks for helping me out man, it helped a lot
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    That's the "BlinkRefreshTime" variable.