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