Search Unity

Pivot Rotation

Discussion in 'Scripting' started by GeneralBazooka, Sep 23, 2012.

  1. GeneralBazooka

    GeneralBazooka

    Joined:
    Sep 23, 2012
    Posts:
    5
    Hi Folks,

    I needed to do a box rotate around a pivot, I know its trivial and there are other posts about it but I did a little script that does the job, I hope it can help someone else out there, that's it.

    http://www.youtube.com/watch?v=Z9O0u0EmtiI&feature=youtu.be
    Code (csharp):
    1.  
    2.  
    3. /*
    4.  _____                           _  ______                      _        
    5. |  __ \                         | | | ___ \                    | |        
    6. | |  \/ ___ _ __   ___ _ __ __ _| | | |_/ / __ _ _______   ___ | | ____ _
    7. | | __ / _ \ '_ \ / _ \ '__/ _` | | | ___ \/ _` |_  / _ \ / _ \| |/ / _` |
    8. | |_\ \  __/ | | |  __/ | | (_| | | | |_/ / (_| |/ / (_) | (_) |   < (_| |
    9.  \____/\___|_| |_|\___|_|  \__,_|_| \____/ \__,_/___\___/ \___/|_|\_\__,_|
    10.  
    11. ==***==                                                                                                                      
    12. * In a lazy sunday day.
    13. */                      
    14.  
    15. using UnityEngine;
    16. using System.Collections;
    17.  
    18. public class RotateAroundPivot : MonoBehaviour
    19. {
    20.     public Vector3 Pivot;
    21.     public bool DebugInfo = true;
    22.    
    23.     //it could be a Vector3 but its more user friendly
    24.     public bool RotateX = false;
    25.     public bool RotateY = true;
    26.     public bool RotateZ = false;
    27.    
    28.     void FixedUpdate()
    29.     {
    30.         transform.position += (transform.rotation*Pivot);
    31.        
    32.         if (RotateX)
    33.             transform.rotation *= Quaternion.AngleAxis(45*Time.deltaTime, Vector3.right);
    34.         if (RotateY)
    35.             transform.rotation *= Quaternion.AngleAxis(45*Time.deltaTime, Vector3.up);
    36.         if (RotateZ)
    37.             transform.rotation *= Quaternion.AngleAxis(45*Time.deltaTime, Vector3.forward);
    38.  
    39.         transform.position -= (transform.rotation*Pivot);
    40.        
    41.         if (DebugInfo)
    42.         {
    43.             Debug.DrawRay(transform.position,transform.rotation*Vector3.up,Color.black);
    44.             Debug.DrawRay(transform.position,transform.rotation*Vector3.right,Color.black);
    45.             Debug.DrawRay(transform.position,transform.rotation*Vector3.forward,Color.black);  
    46.            
    47.             Debug.DrawRay(transform.position+(transform.rotation*Pivot),transform.rotation*Vector3.up,Color.green);
    48.             Debug.DrawRay(transform.position+(transform.rotation*Pivot),transform.rotation*Vector3.right,Color.red);
    49.             Debug.DrawRay(transform.position+(transform.rotation*Pivot),transform.rotation*Vector3.forward,Color.blue);
    50.         }
    51.     }
    52. }
     
  2. im

    im

    Joined:
    Jan 17, 2013
    Posts:
    1,408
    thanks, just what i needed!

    but how do u save it, i need to do it in like editor so that the object gets the change
     
    Last edited: Aug 12, 2013
  3. felixhcat

    felixhcat

    Joined:
    Oct 21, 2013
    Posts:
    1
  4. performat

    performat

    Joined:
    Feb 4, 2015
    Posts:
    1
    Thank you so much man, it's exactly what I was looking for.
    Merci ;-)
     
  5. lauraaa

    lauraaa

    Joined:
    Dec 30, 2016
    Posts:
    16
    Thanks so much GeneralBazooka it saves my day!!
     
  6. syouleaf

    syouleaf

    Joined:
    Nov 7, 2019
    Posts:
    2
    Wow! You helped me in 2021!
     
  7. UAR777

    UAR777

    Joined:
    Jul 2, 2013
    Posts:
    35
    That's a great solution. I'll have a look at implementing it. :)