Manage Camera Snapping (Cinemachine & New Input System for Touch Input)

Hello everyone,

I’m implementing camera movement (touch) within my tablet application using Cinemachine to manage the controls for an RTS game. The view is from above, with the ability to use one finger to move the camera along the X and Z axes, or two fingers for pinch-to-zoom and camera rotation.

The problem arises when I try to rotate the camera around the target using a two-finger rotation gesture. There’s an issue with snapping: when I place both fingers on the screen, the camera snaps (depending on how I position my fingers), whereas I’d like the camera rotation to start from the current point and increase (or decrease) smoothly with the finger movement.

private void RotateCamera(Touch touch0, Touch touch1)
    {
        if (touch1.delta.magnitude > 0.25f && touch0.delta.magnitude > 0.25f)
        {
            // Calculate the vector between the two touches
            Vector2 v2 = touch1.screenPosition - touch0.screenPosition;

            // Calculate the angle between the vector and the x-axis
            float angle = Vector2.SignedAngle(Vector2.left, v2);

            // Calculate the delta angle
            float deltaAngle = Mathf.DeltaAngle(previousAngle, angle);

            //prevent camera from snapping
            if (Mathf.Abs(deltaAngle) > 10f)
            {
                deltaAngle = 0f;
            }

            // Update the previous angle
            previousAngle = angle;

            // Increment the cameraTarget rotation
            cameraTarget.Rotate(Vector3.up, deltaAngle * rotationSpeed * Time.deltaTime, Space.World);
        }
    }

I found a workaround by checking the camera’s deltaAngle, which helps avoid the snapping effect that was too “obvious.” However, this requires the camera rotation to be done slowly, because if the fingers rotate too quickly, the rotation doesn’t happen (as the deltaAngle becomes too high).

How can I fix this part of the code?

Thank you for your help.

Francesco

I think you need to invalidate previousAngle when the user stops touching.

Exactly this. This can take many forms… easy cheesy is to:

  1. continue computing angle
  2. continue assigning angle to previousAngle (at the end, like you do)

But then only execute the deltaAngle = computation (and its use) when you have two touches this frame AND two touches the previous frame. You would need to track touch count this frame, touch count the previous frame.

This treats that first deltaAngle computation as invalid and disregards it.

Sorry, I probably explained myself poorly:
I already handle the detection of a double-tap (Update Method).

What I would like to implement is for the target’s rotation (with the camera following the target’s rotation) to increase incrementally depending on whether I move my fingers clockwise or counterclockwise on the screen.

Unfortunately, I’ve noticed that depending on how I position the two fingers on the screen when starting the rotation, there’s a sort of snapping effect. Instead, I’d like to simply increase or decrease the target’s rotation smoothly FROM the current target rotation.

Thank you all

Francesco