How to do Camera Shake with Cinemachine!

How to do Camera Shake with Cinemachine!

Code Monkey

4 года назад

87,343 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

DebugJk
DebugJk - 26.07.2023 10:37

who have NullReference error >>> write >> private CinemachineBasicMultiChannelPerlin cinemachineBasicMultiChannelPerlin;

Ответить
Victor Jota
Victor Jota - 24.05.2023 14:58

Nice, that was easy!

Ответить
TankyCatCool
TankyCatCool - 16.04.2023 07:05

help me ples Object reference not set to an instance of an object
ScreenShakeController.ShakeCamera (System.Single intesity, System.Single time) (at Assets/Scripts/ScreenShakeController.cs:19)
Movement.Update () (at Assets/Scripts/Movement.cs:217)

Ответить
Gentuar
Gentuar - 12.04.2023 18:13

Whats the best game you maked ? 🎉

Ответить
chintan shroff
chintan shroff - 27.02.2023 09:38

Wonderful tutorial. Thanks. Is it possible to make this shake in only Y axis?

Ответить
Saurabh Kumar
Saurabh Kumar - 28.01.2023 07:20

Sir, I am getting "Null reference exception" error in the line where we get CinemachineBasicMultiChannelPerlin component.
Please help sir!

Ответить
Simón Bahamonde
Simón Bahamonde - 12.01.2023 16:21

I have a problem using this shake, on a 2D game worked perfectly but now trying in 3D it looks really wobbly after build (played on the editor looks nice tho)... does anyone have any clue on how to solve the wobbliness

Ответить
Albikro Games
Albikro Games - 22.12.2022 21:22

The positions of objects may be offseted a bit after a rough shake. Any solutions?

Ответить
Nathan Ackley
Nathan Ackley - 12.11.2022 02:47

How to deal with multiple cameras and transitioning between them? For some reason when I have Cam 1 working, and I transition to a new room with another camera, the script stops working.

Ответить
GD_Helper
GD_Helper - 30.10.2022 19:34

Thank u so much

Ответить
Moon ..
Moon .. - 22.10.2022 14:26

can i used this code with cinemachinefreelook????

Ответить
Federico Villarruel
Federico Villarruel - 26.09.2022 01:03

If you use FreeLook Camera:

public static CinemachineShake instance;

public float shakeIntensity = 5f;
public float shakeTime = 0.1f;

private CinemachineFreeLook cinemachineFreeLook;
private float shakeTimer;

private void Awake()
{
instance = this;
cinemachineFreeLook = GetComponent<CinemachineFreeLook>();
}

public void ShakeCamera()
{
for(int i = 0; i < 3; i++)
{
CinemachineBasicMultiChannelPerlin cinemachineBasicMultiChannelPerlin = cinemachineFreeLook.GetRig(i).GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
cinemachineBasicMultiChannelPerlin.m_AmplitudeGain = shakeIntensity;
shakeTimer = shakeTime;
}
}

private void Update()
{
if (shakeTimer > 0)
{
shakeTimer -= Time.deltaTime;
if (shakeTimer <= 0f)
{
for (int i = 0; i < 3; i++)
{
CinemachineBasicMultiChannelPerlin cinemachineBasicMultiChannelPerlin = cinemachineFreeLook.GetRig(i).GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
cinemachineBasicMultiChannelPerlin.m_AmplitudeGain = 0f;
}
}
}
}

Ответить
Luca Fernandez
Luca Fernandez - 13.09.2022 20:45

Hey Code Monkey! Great Tutorial, i was wondering, this works when i have more than one cinmeachine camera? I have to add the shake script to the other ones?

Ответить
Raul Jeri Lara
Raul Jeri Lara - 13.09.2022 18:38

Why create an Instance for ShakeCamera and not only put in an object and then call the method?

Ответить
Abazel
Abazel - 29.08.2022 23:29

Hi, I tried my best to follow this but I keep getting an error saying "NullReferenceException: Object reference not set to an instance of an object" at this line "CameraShake.Instance.Shake(5f, 0.1f);" and the camera then doesn't shake

Ответить
Juli Pasi
Juli Pasi - 24.08.2022 20:00

Love the video really helpfull, keep it up

Ответить
James Foward
James Foward - 22.08.2022 00:56

Hi Code Monkey - I've been following your 3rd person controller videos (they're brilliant).
Just wanted your thoughts on what was the best way to implement a CineMachine camera shake when using the 3rd person controller - with regards to the aiming.

Firstly have you covered this in any other video? If so just po

In those tutorials you are using a second camera to blend between Player Follow and Player Aim. The problem comes because the virtual camera responsible for player aim is always targeting the position in front (the debug ball). I believe its recentering the camera and wildly affecting the shake effect.

Any thoughts? Perhaps a 3rd Camera needs to be introduced for Solely camera shake?

Cheers, James 🥰

Ответить
Thrymbor
Thrymbor - 16.08.2022 16:54

i got a weird bug. when iu use this effect everytime i shoot, it starts to tilt the world at some point O.o

Ответить
Sergey Lebedev
Sergey Lebedev - 16.08.2022 08:36

I'm suggesting use coroutine

run like this:
StartCoroutine(nameof(ShakeCamera), new ShakeCameraOptions { DurationInSeconds = 3, AmplitudeGain = 1, FrequencyGain = 1 });

and implement lie this:
private CinemachineBasicMultiChannelPerlin cinemachineBasicMultiChannelPerlin;
private IEnumerator ShakeCamera(ShakeCameraOptions options)
{
//var rigIndex = CinemachineFreeLook.RigNames.Where()
cinemachineBasicMultiChannelPerlin = cinemachineBasicMultiChannelPerlin ?? Cinemachine.GetRig(2).GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();

cinemachineBasicMultiChannelPerlin.m_AmplitudeGain = options.AmplitudeGain;
cinemachineBasicMultiChannelPerlin.m_FrequencyGain = options.FrequencyGain;

yield return new WaitForSeconds(options.DurationInSeconds);

cinemachineBasicMultiChannelPerlin.m_AmplitudeGain = 0;
cinemachineBasicMultiChannelPerlin.m_FrequencyGain = 0;
}

Ответить
Surya Md
Surya Md - 13.08.2022 14:15

Instead of using the singleton pattern I used it diretly in the script I wanted but even though I used getCineMachineComponent on CineMachineVirtual component it is not able to get it on awake and is showing null error

Ответить
BlackBlocky
BlackBlocky - 03.08.2022 23:37

thanks for the great tutorial!

Ответить
Justus Foran
Justus Foran - 19.07.2022 05:03

Thank you for this one. Turns out the cinemachine impulse events only fire if there is a box collider, not a mesh or convex collider. This is the missing piece...now to translate this into visual scripting.

Ответить
Connor Wilson-Long
Connor Wilson-Long - 28.05.2022 07:03

I keep getting the error "Use of unassigned local variable 'cinemachineBasicMultiChannelPerlin" as well as some other errors to do with the mutilchannelperlin. Help.

Ответить
Ygor Ishikawa
Ygor Ishikawa - 12.05.2022 23:31

Nice! Excellent video, thanks!

Ответить
Jordan the Best Lass
Jordan the Best Lass - 06.04.2022 10:08

can't find the "CinemachineShake" when using cinemachine. maybe this tutorial needs an update? i'm using unity 2020

Ответить
Attack of the Nerds
Attack of the Nerds - 20.03.2022 14:40

Thank you so much great tutorial!

Ответить
Voxelheadstudio Indiegamestudio
Voxelheadstudio Indiegamestudio - 19.03.2022 20:05

nice one !

Ответить
EmperX
EmperX - 02.03.2022 17:09

Code for those who need it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CinemachineShake : MonoBehaviour
{
private CinemachineVirtualCamera cinemachineVirtualCamera;
private float shakeTimer;

private void Awake()
{
cinemachineVirtualCamera = GetComponent<CinemachineVirtualCamera>();
}

public void ShakeCamera(float intensity, float time)
{
CinemachineBasicMultiChannelPerlin cinemachineBasicMultiChannelPerlin = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
cinemachineBasicMultiChannelPerlin.m_AmplitudeGain = intensity;
shakeTimer = time;
}

private void Update()
{
if (shakeTimer > 0)
{
shakeTimer -= Time.deltaTime;
if (shakeTimer <= 0f)
{
CinemachineBasicMultiChannelPerlin cinemachineBasicMultiChannelPerlin = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
cinemachineBasicMultiChannelPerlin.m_AmplitudeGain = 0f;
}
}
}
}

Ответить
Philip Hansson
Philip Hansson - 17.02.2022 14:41

Thnx so much =)

Ответить
Chez
Chez - 12.02.2022 17:18

why does my character sprite keep on flickering when I add camera shake?

Ответить
My name is Walter Hartwell White. I live at 308
My name is Walter Hartwell White. I live at 308 - 07.02.2022 02:43

Super Useful! thx

Ответить
Mahdi *Mohamad* Mosavi
Mahdi *Mohamad* Mosavi - 05.02.2022 01:06

Great post helped me alot. Thankyou.

Ответить
Kaldrin
Kaldrin - 21.01.2022 18:59

Thank you!

Ответить
bilqinm
bilqinm - 18.01.2022 22:13

thnx bro

Ответить
Piratical [Ratatoskr]
Piratical [Ratatoskr] - 06.12.2021 21:32

For anyone who had the same issue as I did; the camera wouldn't stop shaking once it started. So had to change:
Mathf.Lerp(startingIntensity, 0f, shakeTimer / shakeTimerTotal);
to this:
Mathf.Lerp(startingIntensity, 0f, 1 - shakeTimer / shakeTimerTotal);
Excellent tutorial though, thank you!

Ответить
Mathew
Mathew - 06.12.2021 03:52

How would you change the profile? I want to use one of the Handheld profiles when I'm not shooting.

Ответить
Coding Kricket
Coding Kricket - 19.11.2021 08:26

Thanks! This helped so much!

Ответить
MoonNeko
MoonNeko - 31.10.2021 16:02

Bruh..
Your typing speed is like a train! WTF

Ответить
Rafik Benamara
Rafik Benamara - 17.10.2021 14:30

If all your video was like this 😍 thank you very simple and clean

Ответить
Juan Pabon
Juan Pabon - 24.09.2021 03:17

Thank you thank you thank you and thank you :) ,.. bro, a question. Why when changing cameras, I go from VCam 1 to Cam 2, I disable Vcam 1 because I am on Vcam2. But when I go back to Vcam 1 and enable, it doesn't work anymore, it's like sticking to VCam2. Because I go back to VCam2 and it keeps working but VCam1 no longer after the change. Any idea?

Ответить
Jorge Iván Ochoa Gómez
Jorge Iván Ochoa Gómez - 21.09.2021 01:40

There is any way to do this using the Cinemachine Impulse listener feature ? Thank you guys

Ответить
Sigurd Vatnehol
Sigurd Vatnehol - 12.09.2021 13:36

hm... This is not really working for me :/ But I have three cams atm stored in a StateDrivenCamera, is there a way to ad screenshake to all of them simultaniously?

Edit: well, I made it work, I just did the same thing but for all cameras instead of one, alittle clunky but worked.

Ответить
Ritle
Ritle - 28.08.2021 21:34

Thank you so much for this tutorial, it really helped me :) keep up the great work!

Ответить
Federico Arrico
Federico Arrico - 06.08.2021 19:04

Very helpful. Problem is when i trigger the cam shake... my camera doesn't go back to its original position :S

Ответить
Christoph
Christoph - 26.07.2021 22:24

Thanks for the tutorial, the lerping part for some reason isn't working for me though. And yes, I changed it to = Mathf.Lerp(startingIntensity, 0f, 1-(shakeTimer/shakeTimerTotal)); Why doesn't it work?

Ответить
caua bozano
caua bozano - 11.07.2021 02:49

MUITO OBRIGADOOOOOO!!!!!!!!!!!!!

Ответить
durrium
durrium - 05.07.2021 11:59

Nice vid! Sometimes it feels like you are over complicating stuff with your coding though

Ответить