Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Multiple Mathf.SmoothSteps in a Coroutine not staying in proportion?

Discussion in 'Scripting' started by somuchforpathos, Apr 2, 2024.

  1. somuchforpathos

    somuchforpathos

    Joined:
    Jan 28, 2022
    Posts:
    17
    I'm trying to make a script that slows down the game speed with Time.timeScale but he maintains their normal movement speed, jump, etc. when using rigidbody.velocity for movement. Before moving to rigidbody movement this was a simple as changing to Time.unscaledDeltaTime, however this doesn't work anymore so I wrote a coroutine that slows time whilst speeding up player movement to match using Mathf.SmoothStep. The issue I'm having is that they don't seem to be staying in proportion with each other which results in the player briefly gaining speed/being able to jump higher/lower during the SmoothStep. My assumption here is that as lerp uses a percentage of the value that they should stay in proportion but that doesn't seem to be the case.

    Code (CSharp):
    1.     private IEnumerator TimeWarpSlowDown(float warpTimeTo, float warpSpeedTo, float warpJumpTo, float transitionTime)
    2.     {
    3.         Debug.LogWarning("Time Warp Slow Down starting at " + Time.time);
    4.  
    5.         float currentTimeScale = Time.timeScale;
    6.         float currentGravity = Physics.gravity.y;
    7.         float currentSpeedScale = playerControllers[0].speedScale;
    8.         float currentJumpScale = playerControllers[0].jumpScale;
    9.         float t = 0f;
    10.  
    11.         while (Time.time < timeWarpTransitionEndTime)
    12.         {
    13.             t += (1 / transitionTime) * Time.unscaledDeltaTime;
    14.  
    15.             // Gradually slow down time over x seconds, using mathf.smoothstep, updating gravity and player speed
    16.             Time.timeScale = Mathf.SmoothStep(currentTimeScale, warpTimeTo, t);
    17.             Physics.gravity = new Vector3(0, Mathf.SmoothStep(currentGravity, currentGravity * warpSpeedTo, t), 0);
    18.             foreach (var player in playerControllers)
    19.             {
    20.                 player.speedScale = Mathf.SmoothStep(currentSpeedScale, warpSpeedTo, t);
    21.                 player.jumpScale = Mathf.SmoothStep(currentJumpScale, warpJumpTo, t);
    22.             }
    23.  
    24.             // The time of physics steps needs to change with time to prevent jerky movement
    25.             Time.fixedDeltaTime = defaultFixedDeltaTime * Time.timeScale;
    26.  
    27.             yield return null;
    28.         }
    29.  
    30.         // After lerping set values to ensure it is set to the correct value
    31.         Time.timeScale = warpTimeTo;
    32.         Physics.gravity = new Vector3(0, currentGravity * warpSpeedTo, 0);
    33.         foreach (var player in playerControllers)
    34.         {
    35.             player.speedScale = warpSpeedTo;
    36.             player.jumpScale = warpJumpTo;
    37.         }
    38.  
    39.         Debug.LogWarning("Time Warp Slow Down finished at " + Time.time);
    40.     }
    Any idea why this is happening?
     
    Last edited: Apr 2, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,611
    somuchforpathos likes this.
  3. somuchforpathos

    somuchforpathos

    Joined:
    Jan 28, 2022
    Posts:
    17
    Ahhh okay, I thought it might be out of sync but couldn't see how, thanks for the help.

    Also re: gravity, it does scale with time which is the issue I was having. Though I increased the move speed and jump velocity to counter the decrease in timeScale, it meant the player would then fall slowly after jumping, so I needed to scale up gravity as timeScale decreased.