Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to pause the game but leave menus unpaused?

Discussion in 'Scripting' started by xonestly, Nov 30, 2023.

  1. xonestly

    xonestly

    Joined:
    Jan 11, 2020
    Posts:
    10
    When a player picks up an item in my game, an animation plays where there is the heading, the animated actual item, then a description. I want it so that when the player picks up an item the game pauses (I think I've figured that out just by doing timescale = 0) but the issue is when I do that the item doesn't get animated properly and the animation doesn't disappear because it is still technically paused, so how can I pause somethings but not pause others? Or am I not thinking about this the right way?

    Heres the code for the item pickup animation:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using TMPro;
    4.  
    5. public class ItemGetAnimation : MonoBehaviour
    6. {
    7.     public string heading = "Heading"; // Variable for heading
    8.     public Sprite itemSprite; // Variable for sprite
    9.     public string description = "Description"; // Variable for description
    10.  
    11.     public TMP_Text headingText; // Reference to the TextMeshPro Text component for heading
    12.     public Image spriteImage; // Reference to the UI Image component for sprite
    13.     public TMP_Text descriptionText; // Reference to the TextMeshPro Text component for description
    14.  
    15.     public Sprite[] animationFrames; // Variable for storing animation frames
    16.     public float frameDuration = 0.5f; // Duration between frames
    17.  
    18.     public float floatAmount = 10f; // Amount of floating movement
    19.     public float floatSpeed = 1f; // Speed of floating movement
    20.  
    21.     private int currentFrameIndex = 0;
    22.     private float lastFrameTime;
    23.     private Vector3 initialImagePosition;
    24.  
    25.     private void Start()
    26.     {
    27.         DeactivateUI(); // Deactivate UI elements initially
    28.         initialImagePosition = spriteImage.rectTransform.localPosition; // Store the initial image position
    29.     }
    30.  
    31.     public void InitializeAnimation()
    32.     {
    33.         ActivateUI(); // Activate UI elements
    34.         InitializeUI(); // Initialize UI content
    35.  
    36.         if (animationFrames != null && animationFrames.Length > 0)
    37.         {
    38.             lastFrameTime = Time.time;
    39.             InvokeRepeating(nameof(Animate), 0, frameDuration); // Start frame-based animation
    40.             InvokeRepeating(nameof(FloatAnimation), 0, Time.deltaTime); // Start floating animation
    41.         }
    42.         else
    43.         {
    44.             Debug.LogError("No frames assigned to animationFrames array!");
    45.         }
    46.  
    47.         Invoke(nameof(DeactivateUI), 5f); // Deactivate UI after 5 seconds
    48.     }
    49.  
    50.     private void InitializeUI()
    51.     {
    52.         if (headingText != null)
    53.         {
    54.             headingText.text = heading; // Set the text for heading
    55.         }
    56.  
    57.         if (spriteImage != null && itemSprite != null)
    58.         {
    59.             spriteImage.sprite = itemSprite; // Set the sprite image
    60.         }
    61.  
    62.         if (descriptionText != null)
    63.         {
    64.             descriptionText.text = description; // Set the text for description
    65.         }
    66.     }
    67.  
    68.     private void DeactivateUI()
    69.     {
    70.         if (headingText != null)
    71.         {
    72.             headingText.gameObject.SetActive(false); // Deactivate heading text
    73.         }
    74.  
    75.         if (spriteImage != null)
    76.         {
    77.             spriteImage.gameObject.SetActive(false); // Deactivate sprite image
    78.         }
    79.  
    80.         if (descriptionText != null)
    81.         {
    82.             descriptionText.gameObject.SetActive(false); // Deactivate description text
    83.         }
    84.     }
    85.  
    86.     private void ActivateUI()
    87.     {
    88.         if (headingText != null)
    89.         {
    90.             headingText.gameObject.SetActive(true); // Activate heading text
    91.         }
    92.  
    93.         if (spriteImage != null)
    94.         {
    95.             spriteImage.gameObject.SetActive(true); // Activate sprite image
    96.         }
    97.  
    98.         if (descriptionText != null)
    99.         {
    100.             descriptionText.gameObject.SetActive(true); // Activate description text
    101.         }
    102.     }
    103.  
    104.     private void Animate()
    105.     {
    106.         if (Time.time - lastFrameTime >= frameDuration)
    107.         {
    108.             // Update UI Image sprite with the current frame
    109.             spriteImage.sprite = animationFrames[currentFrameIndex];
    110.  
    111.             // Move to the next frame
    112.             currentFrameIndex = (currentFrameIndex + 1) % animationFrames.Length;
    113.  
    114.             lastFrameTime = Time.time;
    115.         }
    116.     }
    117.  
    118.     private void FloatAnimation()
    119.     {
    120.         // Calculate the new Y position based on a sine wave for floating effect
    121.         float newY = initialImagePosition.y + Mathf.Sin(Time.time * floatSpeed) * floatAmount;
    122.  
    123.         // Update the UI Image's local position
    124.         spriteImage.rectTransform.localPosition = new Vector3(initialImagePosition.x, newY, initialImagePosition.z);
    125.     }
    126. }
    127.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,311
    Time.unscaledDeltaTime
    is for when you want a delta time not affected by time scale. Otherwise systems like animations usually have a way to choose if they use scaled or unscaled time.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,136
    This is not a question for the 2D forum.

    I'll move your post to the Scripting forum.
     
    Bunny83 likes this.