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. Dismiss Notice

Need this script to work to my needs but need help please.

Discussion in 'Scripting' started by metaldc4life, Sep 4, 2021.

  1. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Hello, :D

    I need to make my script to where when the time counts to zero the time returns back to normal in my slow motion script and when it get to zero out of slow motion it slowly regenerates...


    Thank You For Any Help As It Is Appreciated!

    please help me and stay safe!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class SlowMotion : MonoBehaviour {
    5.  
    6.     float currentAmount = 0f;
    7.  
    8.     float maxAmount = 5f;
    9.    
    10.     public float timeLeft = 3.0f;
    11.    
    12.     public Text startText; // used for showing countdown from 3, 2, 1
    13.    
    14.  
    15. // Update is called once per frame
    16.    
    17.     void Update()
    18.     {
    19.         timeLeft -= Time.deltaTime;
    20.         startText.text = (timeLeft).ToString("0");
    21.         if (timeLeft < 0)
    22.  
    23.             if(Input.GetKeyDown ("q")){
    24.  
    25. if(Time.timeScale == 1.0f)
    26. Time.timeScale = 0.3f;
    27.  
    28. else
    29.  
    30. Time.timeScale = 1.0f;
    31. Time.fixedDeltaTime = 0.02f * Time.timeScale;
    32. }
    33.  
    34.  
    35. if(Time.timeScale == 0.03f){
    36.  
    37. currentAmount += Time.deltaTime;
    38. }
    39.  
    40. if(currentAmount > maxAmount){
    41.  
    42. currentAmount = 0f;
    43. Time.timeScale = 1.0f;
    44.  
    45. }
    46.  
    47. }
    48. }
     
  2. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
  3. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    this is what i want:
    I'm sorry i meant that when you press "q" you are in slow motion but slow motion only last like 5 seconds and then goes back to normal time scale..

    But the slow motion needs to regenerate first in order to use it again... sorry i am not good at explaining things lol.. :)
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    but you haven't explained why this was hard or where your problems lie. you can't expect us to do your homework.
    I also need someone to make me the next Minecraft, and it would be helpful if I was the sole owner. lol
     
    danielesuppo likes this.
  5. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    What do you mean by
    ?

    If you manage your countodown with a Lerp you should be able to do whatever you want with the time scale
    countdown = (int)Mathf.Lerp (3, 0, lerpTime);
     
  6. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    i'll try it and let you guys know thank you! :)

    and what i mean by regenerate i mean like a "cooldown" time...
     
  7. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179

    So where do i place the countdown = (int)Mathf.Lerp (3, 0, lerpTime); ?

    I'm sorry i'm trying! lol

    Some people do not even try but i am trying to learn though! lol

    And forgive me i have a disability that prevents me from explaining things properly... :) (seriously)

    Still need help.
     
    Last edited: Sep 4, 2021
  8. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    metaldc4life likes this.
  9. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    I know a bit it's just i'm having a hard time.. lol :)

    I'll try it again then.. thank you. :D

    LOVE your animals by the way in your profile pic.. :)
     
  10. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    If you can't do that maybe when I'll have some time I could do that for you, but maybe you should not know how to use it. Anyway, if you're in big troubles write here, and explain very very well what you want to realize
     
    metaldc4life likes this.
  11. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Well quite honestly you know what you are doing and i do not I would be very grateful if you did that for me though..
     
  12. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    I do NOT want to sound like a free-loader either i like doing things by myself but when i pull my hair out over something simple like this it discourages me :/

    I would REALLY appreciate it!!!

    :D

    Thank You My Friend!

    (If you are up for it making the script..)

    Have A Good Day And Stay Safe My Friend!
     
  13. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    so you want a 3-stage ability:
    1st stage: ability is ready
    2nd stage: ability is active, ability clock is draining
    3rd stage: ability is inactive, cooldown is filling

    on a timeline

    ready>>-------ability--------|--------------------cooldown-----------------------|

    additionally, during the ability stage, you want the actual passage of time to be slowed.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class SlowDownAbility : MonoBehaviour {
    4.  
    5.   [SerializeField] [Range(0.5f, 5f)] public float abilityTime;
    6.   [SerializeField] [Range(0.5f, 5f)] public float cooldownTime;
    7.   [SerializeField] [Range(0.01f, 1f)] public float slowDown;
    8.  
    9.   AbilityStage stage;
    10.   float timer, savedTime;
    11.  
    12.   private enum AbilityStage {
    13.     Ready,
    14.     InProgress,
    15.     Cooldown
    16.   }
    17.  
    18.   void Awake() {
    19.     timer = 0f;
    20.     setReady();
    21.   }
    22.  
    23.   float elapsed => timer - savedTime;
    24.  
    25.   void setReady() => stage = AbilityStage.Ready;
    26.  
    27.   void Update() {
    28.     if(IsInProgress()) {
    29.       if(elapsed >= abilityTime)
    30.         Deactivate();
    31.     } else if(!IsReady()) { // cooldown
    32.       if(elapsed >= cooldownTime)
    33.         setReady();
    34.     }
    35.  
    36.     timer += Time.unscaledDeltaTime;
    37.   }
    38.  
    39.   // activates the ability only if it was ready; for external use (i.e. key press)
    40.   public void Activate() {
    41.     if(IsReady()) {
    42.       stage = AbilityStage.InProgress;
    43.       savedTime = timer;
    44.       Time.timeScale = slowDown;
    45.     }
    46.   }
    47.  
    48.   // forcefully deactivates the ability currently in progress; for external use
    49.   public void Deactivate() {
    50.     if(IsInProgress()) {
    51.       stage = AbilityStage.Cooldown;
    52.       savedTime = timer;
    53.       Time.timeScale = 1f;
    54.     }
    55.   }
    56.  
    57.   // returns true if the ability is ready to activate, false if not; for external use
    58.   public bool IsReady() => stage == AbilityStage.Ready;
    59.  
    60.   // returns true if the ability is in progress, false if not; for external use
    61.   public bool IsInProgress() => stage == AbilityStage.InProgress;
    62.  
    63.   // returns current time progress percentage (0 .. 1); for external use
    64.   public float GetTimeProgress() {
    65.     if(IsInProgress()) {
    66.       return Mathf.Min(1f, elapsed / abilityTime);
    67.     } else if(!IsReady()) { // cooldown
    68.       return Mathf.Min(1f, elapsed / cooldownTime);
    69.     }
    70.     return 1f;
    71.   }
    72.  
    73. }
     
    Last edited: Sep 4, 2021
    metaldc4life likes this.
  14. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Man!
    That is awesome!!

    One error though, the serialized field's do not work with it..??? lol

    throws a null reference exception for some reason?
     
  15. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    I think that orionsyndrome has made a much better script than what I could do.
     
    metaldc4life likes this.
  16. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    for it to compile i had to change it to a serialized reference on both serialized field's and it compiled correctly.. ;)
     
  17. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I've updated it slightly.
     
  18. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    don't do that. I probably made a typo.
     
  19. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    oh lol ok
     
  20. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    If you have your variables public I think that would not be necessary to use SerializedField, isn't it?
     
    metaldc4life likes this.
  21. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    yes i think so to my knowledge.

    but my knowledge is very limited in this field lol
     
  22. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    it is done for clarity. and it is the recommended practice.
    I just made a typo it's SerializeField, not Serialized
    don't use SerializedReference because you don't know what that's for and it's too early for you to know.
     
    metaldc4life likes this.
  23. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
  24. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Code (CSharp):
    1. timer += Time.deltaUnscaledTime;
    2.  
    the line above does not work?
    Should it be UnscaledDeltaTime instead??


    SlowDownAbility.cs(28,21): error CS0117: 'Time' does not contain a definition for 'deltaUnscaledTime'
     
  25. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    probably, this is from my head.

    I've updated it further for clarity, performance, and convenience. thanks for the heads up.

    edit: fixed
     
    metaldc4life likes this.
  26. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    your welcome,
    I should be thanking YOU though friend! :D

    Thank You once again! :p
     
  27. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    you're welcome.

    in a nutshell it's a basic sequential three-state state machine with some time tracking.
    it doesn't use any lerps, it simply accumulates a timer and uses this as a state trigger, but you can get a proper lerp out of it by using GetProgressTime()

    this is so you can animate some progress bar or something.
    you can also tell if some button is clickable by checking IsReady()

    this is more than sufficient for such a simple behavior, but if you start complicating the state machine it won't be maintainable any more and needs to be redesigned.

    you can also adjust it to allow overlaps of multiple slow down effects. currently, only one is allowed at any time.

    hopefully you don't have that many concurrent abilities, this solution is just fine unless you have dozens of monsters (or more) each running wild with their own abilities like this.

    don't forget that you can attach meaningful events to pop out of such a system, and communicate via events instead of having to tie everything together through direct calls.

    for example, when you need to refresh a button, to make it enabled or disabled you can add
    Code (csharp):
    1. public delegate void AbilityStateHandler(AbilityStage stage);
    2. public event AbilityStateHandler AbilityStateChanged;
    3.  
    4. void setReady() {
    5.   stage = AbilityStage.Ready;
    6.   AbilityStateChanged?.Invoke(stage); // you'd need to find all the places where it changes
    7. }
    and elsewhere you can subscribe
    Code (csharp):
    1. public MyGUI : MonoBehaviour {
    2.  
    3.   SlowDownAbility ability; // it doesn't have to be a class member, just find a way to get to it here
    4.  
    5.   void Awake() {
    6.     ability.AbilityStateChanged += abilityHasChanged;
    7.   }
    8.  
    9.   void OnDestroy() {
    10.     ability.AbilityStateChanged -= abilityHasChanged;
    11.   }
    12.  
    13.   void abilityHasChanged(AbilityStage stage) {
    14.     Debug.Log("stage is now: " + stage.ToString());
    15.   }
    16.  
    17. }
    this way you only react to changes throughout the code instead of being always busy for no reason.

    for this to work, however, paste
    private enum AbilityStage { ... }
    to a better place and turn it
    public
     
    Last edited: Sep 4, 2021
    metaldc4life likes this.
  28. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Man,
    that is freakin' awesome!!!

    one LAST question though lol

    Where can i integrate the "q" button to activate it lol!
     
  29. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    and this is the only ability my player will have at the moment.. :)
     
  30. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179

    oh ohhhh i see it now never mind! sorry friend!
     
  31. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    have fun
     
    metaldc4life likes this.
  32. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    thank you!