Search Unity

2D Rotation Lags.

Discussion in 'Scripting' started by ErmergerdEnt, Oct 2, 2019.

  1. ErmergerdEnt

    ErmergerdEnt

    Joined:
    Apr 7, 2017
    Posts:
    54
    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):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5. public class DragRotate : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerDownHandler
    6. {
    7.     public static DragRotate instance;
    8.     // This event echoes the new local angle to which we have been dragged
    9.     public event Action<Quaternion> OnAngleChanged;
    10.     Quaternion _object;
    11.  
    12.     Quaternion dragStartRotation;
    13.     Quaternion dragStartInverseRotation;
    14.    
    15.     public GameObject _target;
    16.  
    17.    
    18.     public Quaternion _newPos;
    19.     public Transform _panel;
    20.    
    21.  
    22.     private void Awake()
    23.     {
    24.      
    25.         _object = _target.transform.rotation;
    26.      
    27.         instance = this;
    28.         // As an example: rotate the attached object
    29.         OnAngleChanged += (rotation) => _target.transform.localRotation = rotation;
    30.      
    31.         //_this = transform.rotation;
    32.     }
    33.     private void Update()
    34.     {
    35.      
    36.     }
    37.  
    38.     // This detects the starting point of the drag more accurately than OnBeginDrag,
    39.     // because OnBeginDrag won't fire until the mouse has moved from the point of mousedown
    40.     public void OnPointerDown(PointerEventData eventData)
    41.     {
    42.        
    43.         dragStartRotation = _target.transform.localRotation;
    44.        
    45.         Vector3 worldPoint;
    46.         if (DragWorldPoint(eventData, out worldPoint))
    47.         {
    48.             //FollowRotation.instance.NewRot();
    49.             // We use Vector3.forward as the "up" vector because we assume we're working in a Canvas
    50.             // and so mostly care about rotation around the Z axis
    51.             dragStartInverseRotation = Quaternion.Inverse(Quaternion.LookRotation(worldPoint - _target.transform.position, Vector3.forward));
    52.          
    53.         }
    54.         else
    55.         {
    56.             Debug.LogWarning("Couldn't get drag start world point");
    57.         }
    58.     }
    59.  
    60.     public void OnBeginDrag(PointerEventData eventData)
    61.     {
    62.         // Do nothing (but this has to exist or OnDrag won't work)
    63.     }
    64.     public void OnEndDrag(PointerEventData eventData)
    65.     {
    66.        
    67.        
    68.         //_target.transform.rotation = _object;
    69.         //_meme.transform.rotation = _object2;
    70.  
    71.     }
    72.  
    73.     public void OnDrag(PointerEventData eventData)
    74.     {
    75.         Vector3 worldPoint;
    76.         if (DragWorldPoint(eventData, out worldPoint))
    77.         {
    78.            
    79.            
    80.            
    81.             Quaternion currentDragAngle = Quaternion.LookRotation(worldPoint - _target.transform.position, Vector3.forward);
    82.            // Quaternion currentDragAngle2 = Quaternion.LookRotation(worldPoint - _meme.transform.position, Vector3.forward);
    83.             if (OnAngleChanged != null)
    84.             {
    85.                 OnAngleChanged(currentDragAngle * dragStartInverseRotation * dragStartRotation);
    86.              
    87.  
    88.             }
    89.         }
    90.      
    91.     }
    92.  
    93.  
    94.     // Gets the point in worldspace corresponding to where the mouse is
    95.     private bool DragWorldPoint(PointerEventData eventData, out Vector3 worldPoint)
    96.     {
    97.         return RectTransformUtility.ScreenPointToWorldPointInRectangle(
    98.             GetComponent<RectTransform>(),
    99.             eventData.position,
    100.             eventData.pressEventCamera,
    101.             out worldPoint);
    102.     }
    103. }
     
  2. ErmergerdEnt

    ErmergerdEnt

    Joined:
    Apr 7, 2017
    Posts:
    54
    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.