Search Unity

Resolved Clamp for cinemachine

Discussion in 'Cinemachine' started by svenvanh, May 15, 2020.

  1. svenvanh

    svenvanh

    Joined:
    Nov 29, 2019
    Posts:
    51
    Hi, I'm using this code to make sure that the camera (without cinemachine) follows the player and stays between y: 0.5 and 20
    Code (CSharp):
    1. transform.position = new Vector3(redBird.transform.position.x, Mathf.Clamp(redBird.transform.position.y, 0.5f, 20f), -11);
    But is it possible to do that with cinemachine? because the way that cinemachine follows the player is 1000x better.
    To be clear I want that cinemachine follows the player on the X and Y axis but when the player is at Y=20 or higher. The camera/cinemachine stays at his Y position but still follows the X position of the player. and when the player is under Y = 20 again the camera Y position is the same as the players Y position.

    I hope you understand what I'm trying to get if not please respond and I'll try it to explain again!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    You can use a CinemachineConfiner extension to confine the camera to remain inside a bounding shape.

    Alternatively, you can write a simple custom extension to apply your constraints to the camera. Here is an example of one that I had lying around that constrains the camera Z. You can copy it and make the appropriate modifications so that it does your Y clamping instead.
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An add-on module for Cinemachine Virtual Camera that locks the camera's Z co-ordinate
    6. /// </summary>
    7. [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class LockCameraZ : CinemachineExtension
    9. {
    10.     [Tooltip("Lock the camera's Z position to this value")]
    11.     public float m_ZPosition = 10;
    12.  
    13.     protected override void PostPipelineStageCallback(
    14.         CinemachineVirtualCameraBase vcam,
    15.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    16.     {
    17.         if (stage == CinemachineCore.Stage.Body)
    18.         {
    19.             var pos = state.RawPosition;
    20.             pos.z = m_ZPosition;
    21.             state.RawPosition = pos;
    22.         }
    23.     }
    24. }
     
  3. svenvanh

    svenvanh

    Joined:
    Nov 29, 2019
    Posts:
    51
    thanks, Gregoryl for the reply! I got it to work! :D

    For the people that also want this. follow these steps!
    1. Go to CM vcam, scroll down in the Inspector until you see
    upload_2020-5-15_16-44-21.png

    2. Click on "(select)" and choose "Cinemachine Confiner"

    3. create an empty GameObject and add a Polygon Collider 2D

    4. Click on this button and make the collider the shape you want.
    upload_2020-5-15_16-48-21.png

    5. Go back to your CM vcam and drag the empty GameObject in here: upload_2020-5-15_16-49-27.png

    6. Now it should work!

    small tip: enable "Is Trigger" in the Polygon Collider 2D so your player/GameObject doesn't get pushed out! upload_2020-5-15_16-51-33.png
     
  4. TomsTales

    TomsTales

    Joined:
    Nov 3, 2020
    Posts:
    91
    I ***** love you.
     
    svenvanh likes this.