I am new to Unity and I created a game now I want to convert the camera controller inputs from mouse to touch swipes but it is not working properly. When I gently touch the screen the camera moves in round circles around the player very bizarrely, I can’t figure out why is it doing it like this. Here is the code of how I am trying to do it:
using UnityEngine;
public class cameraContoller : MonoBehaviour
{
float rotx;
float rotY;
public Vector3 offsett;
public float rotationSpeed = 70;
public float mouseRotationSmoothing = 0.1f; // Smoothing factor for mouse rotation
public Transform playerT;
private Vector2 initialMousePosition;
private Vector2 accumulatedMouseDelta;
void LateUpdate()
{
// Check for mouse input
if (Input.GetMouseButtonDown(0))
{
initialMousePosition = Input.mousePosition;
accumulatedMouseDelta = Vector2.zero;
}
else if (Input.GetMouseButton(0))
{
Vector2 mouseDelta = (Vector2)Input.mousePosition - initialMousePosition;
// Accumulate mouse delta over time with smoothing
accumulatedMouseDelta = Vector2.Lerp(accumulatedMouseDelta, mouseDelta, mouseRotationSmoothing);
// Calculate rotation based on accumulated mouse movement
rotx += -accumulatedMouseDelta.y * rotationSpeed * Time.deltaTime;
rotx = Mathf.Clamp(rotx, -20, 45);
rotY += accumulatedMouseDelta.x * rotationSpeed * Time.deltaTime;
}
Quaternion targetRotation = Quaternion.Euler(rotx, rotY, 0);
transform.position = playerT.position - targetRotation * offsett;
transform.rotation = targetRotation;
}
public Quaternion Yrot => Quaternion.Euler(0, rotY, 0);
}
Can anybody tell me what is the issue with the camera movements and why it acts weirdly when i try to move the camera.