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 how to call GenerateImpulse with unscaled time?

Discussion in 'Cinemachine' started by Mehran_Akhavan, Dec 13, 2022.

  1. Mehran_Akhavan

    Mehran_Akhavan

    Joined:
    Sep 25, 2021
    Posts:
    7
    Is it possible to GenerateImpulse in CinemachineImpulseSource using unscaled time? I implemented a HitStop which when the player is damaged sets Time.timeScale to zero and after some time returns it to one. At the same time I want to generate an impulse for camera shake. But when timeScale is zero my virtual camera noise stops till timeScale backs to one. I saw the “Ignore Time Scale” tick on CinemachineBrain. But I think the problem is in GenerateImpulse which is a different component.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    You can set CinemachineImpulseManager.Instance.IgnoreTimeScale = true
     
    sakamoto-kun likes this.
  3. Mehran_Akhavan

    Mehran_Akhavan

    Joined:
    Sep 25, 2021
    Posts:
    7
    Thanks for your response. I tried it on my game and it seems to have some weird problems and didn’t work correctly. To reproduce the problem I created a new project with unity 2022.2.0f1 and added a simple rigidbody and floor and a virtual camera with Cinemachine Impulse Listener.
    Then write this simple code to test changing Time.timeScale:
    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     private CinemachineImpulseSource impulseSource;
    4.  
    5.     void Awake()
    6.     {
    7.         impulseSource = GetComponent<CinemachineImpulseSource>();
    8.         CinemachineImpulseManager.Instance.IgnoreTimeScale = true;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.W))
    14.             impulseSource.GenerateImpulse(1);
    15.  
    16.         if (Input.GetKeyDown(KeyCode.S)) {
    17.             if (Time.timeScale == 1)
    18.                 Time.timeScale = 0;
    19.             else
    20.                 Time.timeScale = 1;
    21.         }
    22.     }
    23. }
    When the timeScale is 1 impulse works fine when pressing W. But when it’s zero the camera doesn’t shake.

    The really weird thing is if I delete rigidbody2D from the player object it works fine when the timeScale is 0. When I have a rigidbody2D the box falls on the floor.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    Can you send me the test project?
     
  5. Mehran_Akhavan

    Mehran_Akhavan

    Joined:
    Sep 25, 2021
    Posts:
    7
  6. Mehran_Akhavan

    Mehran_Akhavan

    Joined:
    Sep 25, 2021
    Posts:
    7
    Were you able to reproduce the bug? I think when VCam moves the impulse listener doesn't work correctly.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    Thanks for the upload. The problem is that because the player is animating on FixedUpdate, the vcam tracking is also running on FixedUpdate, and when Time.timeScale==0, there are no FixedUpdate calls.

    The solution is to enable Interpolation in your RigidBody. That way, the transform will be modified on the Update clock, so the vcam will run in LateUpdate, and the impulse will start working again when Time.timecale==0. It's best practice to enable interpolation anyway, as it will give you smoother movement.

    upload_2022-12-20_16-19-37.png
     
    Last edited: Dec 21, 2022
    sakamoto-kun likes this.
  8. Mehran_Akhavan

    Mehran_Akhavan

    Joined:
    Sep 25, 2021
    Posts:
    7
    Thanks for your response