Cinemachine (three) automatically select/switch the most suitable camera (Cinemachine Clear Shot Camera)

Preface

In many puzzle-solving games, there are often many secret rooms in the scene. Players need to shuttle back and forth in different rooms. In addition to the third-person camera, we can also arrange different cameras in different rooms and act as roles After arriving at the designated room, turn on the corresponding Camera for processing. The similar effect is as follows:

It can be seen from the Gif that when our character arrives in a different room, our Camera will automatically switch to observe our character. In the past, we may need to write some triggers ourselves to determine which room the character went to, and then turn on the specified Camera. But with Cinemachine, we don't need to have to type code so troublesomely, and we only need to make some simple configuration, Cinemachine will automatically realize the above functions for us.

In this case, we need to use Cinemachine's Clear Shot function, then let us first understand Clear Shot.

 

Clear Shot

Introduction

Clear Shot Camera is a management type of Camera. There will be one or more VirtualCamera in its root directory. ClearShot will select the VirtualCamera with the best picture quality for the observed target. Therefore, using this function when there are many cameras in the scene can ensure that our screen can clearly see our target.

As for how to identify which VirtualCamera has better picture quality, you need to use the Cinemachine Collider mentioned in our previous article. It will analyze the obstacles in the screen to get the ShotQuality value, and use the value of the optimal target distance we set for reference, and select the best VirtualCamera as the current Live state based on this information. If these values ​​are the same, it will be selected according to the priority ( priority ) set in ClearShot for each VirtualCamera .

Therefore, each VirtualCamera under ClearShot must have a Cinemachine Collider component, but we can also add Cinemachine Collider to the ClearShot Camera itself instead, so that there is no need to add each VirtualCamera.

In addition, we can still specify the blending effect between them for each VirtualCamera in ClearShot.

 

Creation method

In the Unity toolbar, select Cinemachine->Create ClearShot Camera to create our ClearShot Camera:

 

After creation, a CM ClearShot will be generated in the scene, and a VirtualCamera will be generated by default in its root directory:

We can continue to add VirtualCamera under ClearShot, they will be automatically recognized in the CinemachineClearShot component, and the priority of each VirtualCamera can be set here. The larger the value, the higher the priority.

There will be a small yellow warning standard, and the corresponding prompt is also given at the top of the component:

This problem is what we mentioned earlier, it is necessary to add Cinemachine Collider to ClearShot or all VirtualCamera included in ClearShot.

 

Attributes

Since ClearShot itself is also inherited from CinemachineVirtualCameraBase , its properties are basically similar to VirtualCamera, as shown below:

Not too much introduction here, you can check the first article of this series .

It should be noted that the Look At and Follow properties of ClearShot. If VirtualCamera itself sets these two properties, then the value set by VirtualCamera itself will be used. If VirtualCamera is not set, the value set by ClearShot is used.

 

application

We first create a simple scene for testing, and then add a capsule body as our character, and set a different Layer for the character to distinguish it from our wall, for example in this case:

Then we add a ClearShot Camera, the default VirtualCamera can look at the current position of our character. Then add some new VirtualCamera under ClearShot, place them in each room, and look at their own room. Because the working principle of ClearShot is to select the current VirtualCamera based on the picture quality calculated by the obstacle. Therefore, we must try our best to ensure that the VirtualCamera in each room will not see other rooms when it is rotating. For example, when our character runs from room A to room B, the camera in room A can still clearly see the character in room B, then ClearShot will not start VirtualCamera in room B. We can make the wall higher during testing.

Note: We can adjust the angle of view in the Scene window first, and then create VirtualCamera, so that the picture of VirtualCamera will be consistent with the picture in our current Scene window. Or first create a VirtualCamera, then adjust the perspective of the Scene window, then select our VirtualCamera, click GameObject->Align With View in the toolbar to adjust our camera position.

Next, let's configure our ClearShot. We first add CinemachineCollider to it, and delete the CinemachineCollider component of the child VirtualCamera. At the same time, set the Look At of ClearShot as our character. Don't set the Follow attribute first, otherwise these VirtualCamera will follow the character and move away from the original position we placed.

If we want the Camera in each room to move with the character, but not to run to other rooms, we can add a CinemachineConfiner component to each VirtualCamera to handle it. CinemachineConfiner can add an activity range to VirtualCamera, and Camera can only move within this range, the specific follow-up introduction.

After the above steps are completed, we can move our character to observe the camera switching effect. The specific effect is similar to the Gif at the beginning of the article.

 

Optimal Target Distance

From the last article CinemachineCollider mentioned this attribute, it is also mentioned in this article. So what is the use of this attribute?

Let us first look at the following situation:

Our character moved from room A to room B, but since the camera in room A can still see our character clearly at this time, the camera in room B cannot be activated. So how can we start the camera in our room B in this situation? At this point, our Optimal Target Distance property can be used.

When introducing this attribute earlier, when the distance from the target to the Camera is closer to the value we set, the higher the score. This score will affect our choice of ClearShot. In the above example, when the character enters room B, it is obviously closer to the Camera in room B at this moment, assuming that the distance is 3 at this time. Then we set the value of Optimal Target Distance to 3. After taking a look at the situation, we will find that in the character room B, although both cameras can clearly see the character, the distance between the camera and the character in room B is 3 It is closer, so the Camera in room B will be activated. The renderings are as follows:

However, because the Optimal Target Distance function will cause certain performance pressure, we can use Priority to better solve this problem.

 

Priority

In addition to setting the Optimal Target Distance to solve the above situation, we can also solve the problem by setting the Priority property of different VirtualCamera in ClearShot.

In the same way, when the Cameras in room A and room B can clearly see the character, that is, when the value of ShotQuality is the same, ClearShot will give priority to the VirtualCamera with the higher Priority value. Therefore, we only need to set the Priority value of the VirtualCamera of room B to be higher than that of room A to achieve the above situation. When the character enters room B, the Camera of room B is activated.

 

to sum up

Clear Shot can help us select the best camera to display among multiple cameras. But because even if the VirtualCamera is not used, it will always look at our goal and calculate the ShotQuality, so there will definitely be some consumption. If in a complex scene, and there are multiple VirtualCamera under Clear Shot, it will inevitably cause certain performance pressure, so it needs to be used according to the situation.

 

Guess you like

Origin blog.csdn.net/wangjiangrong/article/details/109057644