Unity uses Cinemachine plug-in to implement camera following and vibration

 1. Realize following

1. Add plug-ins in PackageManager

2. Create a Cinemachine camera. My project is a 2D project, so I create a 2D camera.

3. Drag Player to Follow and LookAt

 4. Create an empty object, add PolygonCollider2D to it, adjust the size of the visual range and select it in AddExtensions of CinemachineVirtuaCamera's Extensions.

Drag the empty object you just created up to limit the camera's movement range.

2. Realize vibration

1. Select to add this component in select 

2. Create an empty object and add this component to it

Can preview and adjust vibration effects 

3. Create a camera control script and add it to the camera

public class CameraController : MonoBehaviour
{
    private static CameraController instance;
    public static CameraController Instance => instance;
    private void Awake()
    {
        instance = this;
    }
    public CinemachineImpulseSource impulseSource;

    public void PlayerShakeAnimation()
    {
        impulseSource.GenerateImpulse();
    }
}

Calling this GenerateImpulse can trigger vibration. I wrote the camera script in singleton mode for easy calling.

4. Call this PlayerShakeAnimation method when injured to achieve vibration.

Guess you like

Origin blog.csdn.net/holens01/article/details/131900165