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

Question cinemachine and ripple script/shake effect

Discussion in 'Cinemachine' started by IkarusBR, Jun 20, 2023.

  1. IkarusBR

    IkarusBR

    Joined:
    May 12, 2023
    Posts:
    8
    i was working with a simple main camera based system, using a follow script, but ive discovered cinemachine and wanted to use it rather than a simple script, because it looks like itll be helpful later. theres one problem, 2 of my camera effects dont work anymore because i was using the main camera. tried changing the tags, didnt work - tried referencing to a virtualcinemachine, dunno if i did it right but it didnt work. i cant live without my little ripple effect :(
    - i'll not be showing the entire code (so its easier to read, also it has like 320 lines it would probably just be confusing). the ripple effect is a script of keijiro (specifically https://github.com/keijiro/RippleEffect#readme) with a few modifications to fit my project better.

    Code (CSharp):
    1. public IEnumerator Dash()
    2.     {
    3.         audeo.pitch = 1f;
    4.         audeo.PlayOneShot(clip[1]);
    5.         podeAirDash = false;
    6.         podeDash = false;
    7.         executandoDash = true;
    8.         var originalConstraints = rb.constraints;
    9.         float dashAtual = correndo ? (velocidadeDash + boostDashCorrendo + momentum) : velocidadeDash;
    10.         if (dashAtual > velocidadeDash)
    11.             dashCorrendo = true;
    12.         rb.constraints = originalConstraints | RigidbodyConstraints2D.FreezePositionY;
    13.         td.emitting = true;
    14.         rb.velocity = new Vector2(transform.localScale.x * dashAtual, 0f);
    15.         Camera.main.transform.DOComplete();
    16.         Camera.main.transform.DOShakePosition(2f, 0.1f, 8, 90, false, true);
    17.         float duracaoDash = correndo ? (dashTempo * 0.7f) : dashTempo;
    18.         dashParticle.Play();
    19.         ghost.ShowGhost();
    20.         FindObjectOfType<RippleEffect>().Emit(Camera.main.WorldToViewportPoint(transform.position));
    21.         yield return new WaitForSeconds(duracaoDash);
    22.         dashParticle.Stop();
    23.         if (momentum < maxMomentum * 0.5f && correndo)
    24.             momentum *= 1.5f;
    25.         rb.constraints = originalConstraints;
    26.         td.emitting = false;
    27.         executandoDash = false;
    28.         dashEmCooldown = true;
    29.         yield return new WaitForSeconds(cooldownDash);
    30.         dashEmCooldown = false;
    31.         podeDash = true;
    32.     }
    here are prints of my hierarchy and components:

    upload_2023-6-19_22-30-46.png upload_2023-6-19_22-30-57.png
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,207
    The thing to understand is that CinemachineBrain writes the Camera transform in LateUpdate, and CinemachineBrain has a late execution order. Any modifications you make to the Camera's transform will simply be overwritten, unless you make those modifications after the CMBrain has updated the transform. To to this, you can use CinemachineCore.CameraUpdatedEvent, which fires every immediately after the camera's transform is written. At that point you can modify it, and your modification will stick until the next frame.

    Another alternative: refactor the ripple script as a CinemachineExtension, and add it to your vcam.

    Another alternative: create a virtual camera, set its Aim and Body to "Do Nothing". This will create a passive vcam, which is a vcam that has no procedural motion - it simply goes where its transform tells it to. Then, add your old camera script to that, and add the ripple effect to that as well. This vcam - controlled by your custom script - will then fully control the main camera (via the CMBrain), and yet still be part of the Cinemachine ecosystem.
     
    Last edited: Jun 20, 2023
  3. IkarusBR

    IkarusBR

    Joined:
    May 12, 2023
    Posts:
    8
    i'm kind of a noob with cinemachine. i have no clue on how to actually do what you told me to do. sorry to ask this but can you please break it down into steps?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,207
    I feel like that is already broken down into steps. Do you have a specific question?
     
  5. IkarusBR

    IkarusBR

    Joined:
    May 12, 2023
    Posts:
    8
    what exactly do i need to add to my code and what exactly do i need to add to the hierarchy? sorry, i just couldnt understand well what you said:(
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,207
    1. create a virtual camera
    2. set its Aim and Body to "Do Nothing". This will create a passive vcam, which is a vcam that has no procedural motion - it simply goes where its transform tells it to.
    3. add your old camera script to that vcam
    4. add the ripple effect script to that vcam as well.
    This vcam - controlled by your custom script - will then fully control the main camera (via the CMBrain), and yet still be part of the Cinemachine ecosystem.