I have a script that i use to drag and rotate an object using a rotate icon. The script rotates just fine, except for the fact that it lags. When i scale the object up and rotate again, it lags even worse. Any way to make this script rotate more smoothly? Code (CSharp): using System; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class DragRotate : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerDownHandler { public static DragRotate instance; // This event echoes the new local angle to which we have been dragged public event Action<Quaternion> OnAngleChanged; Quaternion _object; Quaternion dragStartRotation; Quaternion dragStartInverseRotation; public GameObject _target; public Quaternion _newPos; public Transform _panel; private void Awake() { _object = _target.transform.rotation; instance = this; // As an example: rotate the attached object OnAngleChanged += (rotation) => _target.transform.localRotation = rotation; //_this = transform.rotation; } private void Update() { } // This detects the starting point of the drag more accurately than OnBeginDrag, // because OnBeginDrag won't fire until the mouse has moved from the point of mousedown public void OnPointerDown(PointerEventData eventData) { dragStartRotation = _target.transform.localRotation; Vector3 worldPoint; if (DragWorldPoint(eventData, out worldPoint)) { //FollowRotation.instance.NewRot(); // We use Vector3.forward as the "up" vector because we assume we're working in a Canvas // and so mostly care about rotation around the Z axis dragStartInverseRotation = Quaternion.Inverse(Quaternion.LookRotation(worldPoint - _target.transform.position, Vector3.forward)); } else { Debug.LogWarning("Couldn't get drag start world point"); } } public void OnBeginDrag(PointerEventData eventData) { // Do nothing (but this has to exist or OnDrag won't work) } public void OnEndDrag(PointerEventData eventData) { //_target.transform.rotation = _object; //_meme.transform.rotation = _object2; } public void OnDrag(PointerEventData eventData) { Vector3 worldPoint; if (DragWorldPoint(eventData, out worldPoint)) { Quaternion currentDragAngle = Quaternion.LookRotation(worldPoint - _target.transform.position, Vector3.forward); // Quaternion currentDragAngle2 = Quaternion.LookRotation(worldPoint - _meme.transform.position, Vector3.forward); if (OnAngleChanged != null) { OnAngleChanged(currentDragAngle * dragStartInverseRotation * dragStartRotation); } } } // Gets the point in worldspace corresponding to where the mouse is private bool DragWorldPoint(PointerEventData eventData, out Vector3 worldPoint) { return RectTransformUtility.ScreenPointToWorldPointInRectangle( GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out worldPoint); } }
Well, its laggy becuase its trying to rotate on all axis instead of just z... So scratch my previous comment. I have to figure out a new approach.