Advertisement
Guest User

RotateMV

a guest
Oct 20th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DragRotateX : MonoBehaviour
  5. {
  6.     public Vector3 selectedAxis = Vector3.one;
  7.     public float speed = 5;
  8.  
  9.    
  10.  
  11.     private Quaternion originalRotation;
  12.     private float startAngle = 0;
  13.     public Vector3 tempVector;
  14.  
  15.     public void Start()
  16.     {
  17.         originalRotation = this.transform.rotation;
  18.     }
  19.     public void Update()
  20.     {
  21.         if (Input.GetMouseButtonDown(0))
  22.         {
  23.            MouseDown();
  24.         }
  25.  
  26.         //This block of code should be the last 2 things in your update
  27.         if (Input.GetMouseButton(0))
  28.         {
  29.            MouseHeld();
  30.            return;
  31.         }
  32.         transform.localEulerAngles = RotateAxisToNearestSide(transform.localEulerAngles, selectedAxis);
  33.     }
  34.  
  35.    Vector3 RotateAxisToNearestSide(Vector3 eulerAngles, Vector3 selectedAxis)
  36.     {
  37.         Vector3 rounded = RoundToNearest90Degree(eulerAngles);
  38.         Vector3 slerped = Vector3.Slerp(eulerAngles, rounded, Time.deltaTime * speed);
  39.         return SetSelectedAxis(eulerAngles, slerped, selectedAxis);
  40.     }
  41.  
  42.     Vector3 RoundToNearest90Degree(Vector3 eulerAngles)
  43.     {
  44.         for(int i = 0; i < 3; i++)
  45.         {
  46.             eulerAngles[i] = Mathf.Round(eulerAngles[i] / 90f) * 90f;
  47.         }
  48.         return eulerAngles;
  49.     }
  50.  
  51.     Vector3 SetSelectedAxis(Vector3 original, Vector3 desired, Vector3 selectedAxis)
  52.     {
  53.         for(int i = 0; i < 3; i++)
  54.         {
  55.             if(selectedAxis[i] != 0)
  56.             {
  57.                 selectedAxis[i] = desired[i];
  58.             }else{
  59.                 selectedAxis[i] = original[i];
  60.             }
  61.         }
  62.  
  63.         return selectedAxis;
  64.     }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.     public void MouseDown()
  72.     {
  73.         originalRotation = this.transform.rotation;
  74.         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
  75.         Vector3 vector = Input.mousePosition - screenPos;
  76.         startAngle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
  77.     }
  78.     public void MouseHeld()
  79.     {
  80.         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
  81.         Vector3 vector = Input.mousePosition - screenPos;
  82.         float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
  83.         Quaternion newRotation = Quaternion.AngleAxis(angle - startAngle, this.transform.up);
  84.         newRotation.z = 0;
  85.         newRotation.eulerAngles = new Vector3(0, -newRotation.eulerAngles.y, 0);
  86.         this.transform.rotation = originalRotation * (newRotation);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement