Advertisement
Guest User

Unity Editor Rotation Script

a guest
May 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. [ExecuteInEditMode]
  5. public class EditorRotationScript : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     private float m_rotationRadius = 1f;
  9.  
  10.     [SerializeField]
  11.     [Range(-300.0f, 300.0f)]
  12.     private float m_rotationSpeed = 1f;
  13.  
  14.     private double m_lastTime = 0f;
  15.     private Vector3 m_oP;
  16.     private Quaternion m_oR;
  17.     private float m_currentRotation = 0f;
  18.  
  19.     private void OnEnable()
  20.     {
  21.         EditorApplication.update += Update;
  22.         m_lastTime = EditorApplication.timeSinceStartup;
  23.         m_oP = transform.position;
  24.         m_oR = transform.rotation;
  25.         m_currentRotation = 0f;
  26.     }
  27.  
  28.     private void OnDisable()
  29.     {
  30.         EditorApplication.update -= Update;
  31.         transform.position = m_oP;
  32.         transform.rotation = m_oR;
  33.     }
  34.  
  35.     private void Update()
  36.     {
  37.         double delta = EditorApplication.timeSinceStartup - m_lastTime;
  38.         m_lastTime = EditorApplication.timeSinceStartup;
  39.         m_currentRotation += (float)(m_rotationSpeed * delta);
  40.  
  41.         transform.position = new Vector3(m_oP.x + m_rotationRadius * Mathf.Cos(m_currentRotation), m_oP.y, m_oP.z + m_rotationRadius * Mathf.Sin(m_currentRotation));
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement