Search Unity

Click and Drag Cinemachine 2DCamera.

Discussion in 'Cinemachine' started by RajneeshG, May 13, 2019.

  1. RajneeshG

    RajneeshG

    Joined:
    Jan 3, 2014
    Posts:
    15
    Though I have managed to drag 2DCamera with my previous code which uses,
    Camera.main.ScreenToWorldPoint(Input.mousePosition);

    Problem starts when CinemachineConfiner with PolygonCollider2D is attached.
    Camera perfectly stops at collider end while dragging, but after colliding when we try to drag it in opposite direction it takes sometime and then starts moving.

    This happens because at collider end the dragging code only restrict transform values of MainCamera where as 2DCamera transform values keeps changing. When we start dragging in opposite direction it waits for 2DCamera transform values to match MainCamera's.

    So, which is the way to trigger event from Confiner/PolygonCollider2D in order to stop drag code?

    Here is a complete code attached to a Cinemachine 2DCamera.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DragCamera : MonoBehaviour
    4. {
    5.     public Camera cam;
    6.     Vector3 camPos;
    7.     Vector3 hitStartPos;
    8.     bool drag;
    9.     Vector3 velocity = Vector3.zero;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     void LateUpdate()
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             hitStartPos = cam.ScreenToWorldPoint(Input.mousePosition);
    22.  
    23.             drag = false;
    24.             velocity = Vector3.zero;
    25.         }
    26.  
    27.         if (Input.GetMouseButtonUp(0))
    28.         {
    29.             drag = false;
    30.         }
    31.  
    32.         if (Input.GetMouseButton(0))
    33.         {
    34.             drag = true;
    35.         }
    36.  
    37.         if (drag)
    38.         {
    39.             Vector3 target = hitStartPos - (CalcDistance(cam.ScreenToWorldPoint(Input.mousePosition), transform.position));
    40.            
    41.             transform.position = Vector3.SmoothDamp(transform.position, target, ref velocity, Time.deltaTime * 5f,  10f, Time.deltaTime);
    42.         }
    43.     }
    44.  
    45.     Vector3 CalcDistance(Vector3 a, Vector3 b)
    46.     {
    47.         Vector3 dist = a - b;
    48.         return dist;
    49.     }
    50. }
     
    Last edited: May 14, 2019
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Maybe when you stop dragging, you can snap the transform back to the Camera's position?
     
  3. RajneeshG

    RajneeshG

    Joined:
    Jan 3, 2014
    Posts:
    15
    Yes, I did it with bool check as,
    confiner.CameraWasDisplaced(GetComponent<CinemachineVirtualCamera>());


    But, we need to keep Confiner Damping = 0. Otherwise Camera gets stuck within damping period most of the time. Things get even worse if we try to move in opposite direction without releasing mouse at collider end.

    There is no IsDamping check available? Need to create our own IsDamping check function?
     
    Last edited: May 14, 2019
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    I'm not sure I understand the problem exactly. I don't understand how the camera is getting stuck. Maybe you can post a video clip that demonstrates what's happening?

    Confiner damping allows the camera to move briefly outside the confining shape, then bounce back in. If the camera is outside the shape, then it's damping.