I am working to create a third person camera controls in unity.
every thing working fine, but on first drag my camera snaps its rotation suddenly, after that it works smoothly untill pointer up event. then after next pointer down on drag it snaps again.
Can any one help on this.
below is my code.
using UnityEngine;
using UnityEngine.EventSystems;
public class CameraMobile2 : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
public float rotationSpeed;
private float xAxis;
private float yAxis;
private float minRotation = 10f;
private float maxRotation = 60f;
public Transform playerHead;
public Transform cameraTransform;
public RectTransform touchpadRect;
private bool isDragging = false;
private Vector2 lastDragPosition;
public void OnPointerDown(PointerEventData eventData)
{
isDragging = true;
lastDragPosition = eventData.position;
Debug.Log("pointer Down");
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("pointer drag");
if (!isDragging)
{
return;
}
Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(touchpadRect, eventData.position, null, out localPointerPosition))
{
Vector2 delta = localPointerPosition - lastDragPosition;
lastDragPosition = localPointerPosition;
yAxis += delta.x * rotationSpeed * Time.deltaTime;
xAxis -= delta.y * rotationSpeed * Time.deltaTime;
xAxis = Mathf.Clamp(xAxis, minRotation, maxRotation);
Vector3 targetRotation = new Vector3(xAxis, yAxis);
cameraTransform.eulerAngles = targetRotation;
}
}
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("pointer Up");
isDragging = false;
}
private void Update() {
cameraTransform.position = playerHead.position - cameraTransform.forward * 2f;
}
}
Need help to remove camera snapping issue.
New contributor
Rehan Younas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.