Search Unity

Rotating Cuboid Script

Discussion in 'Scripting' started by IPS, Dec 23, 2012.

  1. IPS

    IPS

    Joined:
    Nov 19, 2012
    Posts:
    7
  2. WeirdoBc

    WeirdoBc

    Joined:
    Dec 8, 2012
    Posts:
    20
    You have to change the "point of rotation", the center around which the cuboid will rotate. Make it the center of the edge.

    I'd like to take a shot at it. I'll make a script and if I can do it, i'll publish it here. Unless someone else makes it first.

    mata ne,
    WeirdoBc
     
  3. WeirdoBc

    WeirdoBc

    Joined:
    Dec 8, 2012
    Posts:
    20
    Ok, So far, I found out that, to be able to change the point of rotation, you have to create an empty gameobject and make it parent of the cuboid. After the movement, it has to transfer the result to the cuboid, and reset itself to zero. So it will be ready for another movement.

    The hardest part is taking into account it's stretched shape.

    I'll continue on it tomorrow...

    mata ne,
    WeirdoBc
     
  4. WeirdoBc

    WeirdoBc

    Joined:
    Dec 8, 2012
    Posts:
    20
    oh, interesting thing when taking notes about where the direction where the top of the cuboid is facing : the first movement direction is where it will be pointing, until a movement in the same direction, or the opposite, happens.

    it's important to know the direction where the top is facing because when you transfer the movement of the parent to the child (after the movement, to reset the parent's rotation) if the top is in a direction or another, it will change which axe will get the new rotation.

    mata ne,
    WeirdoBc
     
  5. WeirdoBc

    WeirdoBc

    Joined:
    Dec 8, 2012
    Posts:
    20
    Did it! Easier than I thought, when you use the Transform.RotateAround function.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public enum Direction {
    6.     North,
    7.     South,
    8.     East,
    9.     West,
    10.     Up
    11. }
    12.  
    13. public class Cuboid : MonoBehaviour {
    14.    
    15.     public float rotationSpeed = 200;
    16.    
    17.  
    18.     private Direction _topDirection;
    19.     private bool _moving;
    20.     private Direction _rotationDirection;
    21.     private Vector3 _pivot;
    22.     private float _totalRotation;
    23.     private Vector3 _axis;
    24.  
    25.     void Start () {
    26.         _moving = false;
    27.         _topDirection = Direction.Up;
    28.     }
    29.    
    30.     void Update () {
    31.         if (_moving){
    32.             float deltaRotation = rotationSpeed*Time.deltaTime;
    33.             if (_totalRotation + deltaRotation >= 90) {
    34.                 deltaRotation = 90 - _totalRotation;
    35.                 _moving = false;
    36.             }
    37.             if ((_rotationDirection == Direction.West)||(_rotationDirection == Direction.North))
    38.                 transform.RotateAround(_pivot,_axis, deltaRotation);
    39.             else transform.RotateAround(_pivot,_axis, -deltaRotation);
    40.            
    41.             _totalRotation += deltaRotation;
    42.         }
    43.         else if(Input.GetKeyUp(KeyCode.W)) Rotate(Direction.North);
    44.         else if(Input.GetKeyUp(KeyCode.A)) Rotate(Direction.West);
    45.         else if(Input.GetKeyUp(KeyCode.S)) Rotate(Direction.South);
    46.         else if(Input.GetKeyUp(KeyCode.D)) Rotate(Direction.East);
    47.  
    48.     }
    49.    
    50.     void Rotate(Direction direction){
    51.         _rotationDirection = direction;
    52.         _moving = true;
    53.         _totalRotation = 0;
    54.        
    55.         switch(_rotationDirection){
    56.         case Direction.East:
    57.             if((_topDirection == Direction.East)||(_topDirection == Direction.West)){
    58.                 _topDirection = Direction.Up;
    59.                 _pivot = transform.position + new Vector3(1,-.5f,0);
    60.             }else if(_topDirection == Direction.Up){
    61.                 _topDirection = Direction.East;
    62.                 _pivot = transform.position + new Vector3(.5f,-1,0);
    63.             }else _pivot = transform.position + new Vector3(.5f,-.5f,0);
    64.             _axis = Vector3.forward;
    65.             break;
    66.         case Direction.West:
    67.             if((_topDirection == Direction.East)||(_topDirection == Direction.West)){
    68.                 _topDirection = Direction.Up;
    69.                 _pivot = transform.position + new Vector3(-1,-.5f,0);
    70.             }else if(_topDirection == Direction.Up){
    71.                 _topDirection = Direction.West;
    72.                 _pivot = transform.position + new Vector3(-.5f,-1,0);
    73.             }else _pivot = transform.position + new Vector3(-.5f,-.5f,0);
    74.             _axis = Vector3.forward;
    75.             break;
    76.         case Direction.North:
    77.             if((_topDirection == Direction.North)||(_topDirection == Direction.South)){
    78.                 _topDirection = Direction.Up;
    79.                 _pivot = transform.position + new Vector3(0,-.5f,1);
    80.             }else if(_topDirection == Direction.Up){
    81.                 _topDirection = Direction.North;
    82.                 _pivot = transform.position + new Vector3(0,-1,.5f);
    83.             }else _pivot = transform.position + new Vector3(0,-.5f,.5f);
    84.             _axis = Vector3.right;
    85.             break;
    86.         case Direction.South:
    87.             if((_topDirection == Direction.North)||(_topDirection == Direction.South)){
    88.                 _topDirection = Direction.Up;
    89.                 _pivot = transform.position + new Vector3(0,-.5f,-1);
    90.             }else if(_topDirection == Direction.Up){
    91.                 _topDirection = Direction.South;
    92.                 _pivot = transform.position + new Vector3(0,-1,-.5f);
    93.             }else _pivot = transform.position + new Vector3(0,-.5f,-.5f);
    94.             _axis = Vector3.right;
    95.             break;
    96.         }
    97.     }
    98. }
    99.  
    Not bad for a begginer, heh?

    mata ne,
    WeirdoBc
     
    Last edited: Dec 24, 2012
  6. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    or you could just simply play an animation when a button is pressed, basicly same thing as what weirdoBC said exept its all in a code
     
  7. WeirdoBc

    WeirdoBc

    Joined:
    Dec 8, 2012
    Posts:
    20
    Try this new version, it works with any scale of cube, like, exemple, x=3, y=5, z=2:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public enum Direction {
    6.     North,
    7.     South,
    8.     East,
    9.     West
    10. }
    11.  
    12. public enum Orientation {
    13.     YUp_XEast,
    14.     YUp_ZEast,
    15.     XUp_YEast,
    16.     XUp_ZEast,
    17.     ZUp_XEast,
    18.     ZUp_YEast
    19. }
    20.  
    21. public class Cuboid : MonoBehaviour {
    22.    
    23.     public float rotationSpeed = 250;
    24.    
    25.     private Orientation _orientation;
    26.     private bool _moving;
    27.     private Direction _rotationDirection;
    28.     private Vector3 _pivot;
    29.     private float _totalRotation;
    30.     private Vector3 _axis;
    31.     private Vector3 _scale;
    32.  
    33.     void Start () {
    34.         _moving = false;
    35.         _orientation = Orientation.YUp_XEast;
    36.         _scale = transform.localScale;
    37.     }
    38.    
    39.     void Update () {
    40.         if (_moving){
    41.             float deltaRotation = rotationSpeed*Time.deltaTime;
    42.             if (_totalRotation + deltaRotation >= 90) {
    43.                 deltaRotation = 90 - _totalRotation;
    44.                 _moving = false;
    45.             }
    46.             if ((_rotationDirection == Direction.West)||(_rotationDirection == Direction.North))
    47.                 transform.RotateAround(_pivot,_axis, deltaRotation);
    48.             else transform.RotateAround(_pivot,_axis, -deltaRotation);
    49.            
    50.             _totalRotation += deltaRotation;
    51.         }
    52.         else if(Input.GetKeyUp(KeyCode.W)) Rotate(Direction.North);
    53.         else if(Input.GetKeyUp(KeyCode.A)) Rotate(Direction.West);
    54.         else if(Input.GetKeyUp(KeyCode.S)) Rotate(Direction.South);
    55.         else if(Input.GetKeyUp(KeyCode.D)) Rotate(Direction.East);
    56.  
    57.     }
    58.    
    59.     void Rotate(Direction direction){
    60.         _rotationDirection = direction;
    61.         _moving = true;
    62.         _totalRotation = 0;
    63.        
    64.         switch(_rotationDirection){
    65.         case Direction.East:
    66.             switch(_orientation){
    67.             case Orientation.XUp_YEast:
    68.                 _pivot = transform.position + new Vector3(_scale.y/2.0f,-_scale.x/2.0f,0);
    69.                 _orientation = Orientation.YUp_XEast;
    70.                 break;
    71.             case Orientation.XUp_ZEast:
    72.                 _pivot = transform.position + new Vector3(_scale.z/2.0f,-_scale.x/2.0f,0);
    73.                 _orientation = Orientation.ZUp_XEast;
    74.                 break;
    75.             case Orientation.YUp_XEast:
    76.                 _pivot = transform.position + new Vector3(_scale.x/2.0f,-_scale.y/2.0f,0);
    77.                 _orientation = Orientation.XUp_YEast;
    78.                 break;
    79.             case Orientation.YUp_ZEast:
    80.                 _pivot = transform.position + new Vector3(_scale.z/2.0f,-_scale.y/2.0f,0);
    81.                 _orientation = Orientation.ZUp_YEast;
    82.                 break;
    83.             case Orientation.ZUp_YEast:
    84.                 _pivot = transform.position + new Vector3(_scale.y/2.0f,-_scale.z/2.0f,0);
    85.                 _orientation = Orientation.YUp_ZEast;
    86.                 break;
    87.             case Orientation.ZUp_XEast:
    88.                 _pivot = transform.position + new Vector3(_scale.x/2.0f,-_scale.z/2.0f,0);
    89.                 _orientation = Orientation.XUp_ZEast;
    90.                 break;
    91.             }
    92.             _axis = Vector3.forward;
    93.             break;
    94.         case Direction.West:
    95.             switch(_orientation){
    96.             case Orientation.XUp_YEast:
    97.                 _pivot = transform.position + new Vector3(-_scale.y/2.0f,-_scale.x/2.0f,0);
    98.                 _orientation = Orientation.YUp_XEast;
    99.                 break;
    100.             case Orientation.XUp_ZEast:
    101.                 _pivot = transform.position + new Vector3(-_scale.z/2.0f,-_scale.x/2.0f,0);
    102.                 _orientation = Orientation.ZUp_XEast;
    103.                 break;
    104.             case Orientation.YUp_XEast:
    105.                 _pivot = transform.position + new Vector3(-_scale.x/2.0f,-_scale.y/2.0f,0);
    106.                 _orientation = Orientation.XUp_YEast;
    107.                 break;
    108.             case Orientation.YUp_ZEast:
    109.                 _pivot = transform.position + new Vector3(-_scale.z/2.0f,-_scale.y/2.0f,0);
    110.                 _orientation = Orientation.ZUp_YEast;
    111.                 break;
    112.             case Orientation.ZUp_YEast:
    113.                 _pivot = transform.position + new Vector3(-_scale.y/2.0f,-_scale.z/2.0f,0);
    114.                 _orientation = Orientation.YUp_ZEast;
    115.                 break;
    116.             case Orientation.ZUp_XEast:
    117.                 _pivot = transform.position + new Vector3(-_scale.x/2.0f,-_scale.z/2.0f,0);
    118.                 _orientation = Orientation.XUp_ZEast;
    119.                 break;
    120.             }
    121.             _axis = Vector3.forward;
    122.             break;
    123.         case Direction.North:
    124.             switch(_orientation){
    125.             case Orientation.XUp_YEast:
    126.                 _pivot = transform.position + new Vector3(0,-_scale.x/2.0f,_scale.z/2.0f);
    127.                 _orientation = Orientation.ZUp_YEast;
    128.                 break;
    129.             case Orientation.XUp_ZEast:
    130.                 _pivot = transform.position + new Vector3(0,-_scale.x/2.0f,_scale.y/2.0f);
    131.                 _orientation = Orientation.YUp_ZEast;
    132.                 break;
    133.             case Orientation.YUp_XEast:
    134.                 _pivot = transform.position + new Vector3(0,-_scale.y/2.0f,_scale.z/2.0f);
    135.                 _orientation = Orientation.ZUp_XEast;
    136.                 break;
    137.             case Orientation.YUp_ZEast:
    138.                 _pivot = transform.position + new Vector3(0,-_scale.y/2.0f,_scale.x/2.0f);
    139.                 _orientation = Orientation.XUp_ZEast;
    140.                 break;
    141.             case Orientation.ZUp_YEast:
    142.                 _pivot = transform.position + new Vector3(0,-_scale.z/2.0f,_scale.x/2.0f);
    143.                 _orientation = Orientation.XUp_YEast;
    144.                 break;
    145.             case Orientation.ZUp_XEast:
    146.                 _pivot = transform.position + new Vector3(0,-_scale.z/2.0f,_scale.y/2.0f);
    147.                 _orientation = Orientation.YUp_XEast;
    148.                 break;
    149.             }
    150.             _axis = Vector3.right;
    151.             break;
    152.         case Direction.South:
    153.             switch(_orientation){
    154.             case Orientation.XUp_YEast:
    155.                 _pivot = transform.position + new Vector3(0,-_scale.x/2.0f,-_scale.z/2.0f);
    156.                 _orientation = Orientation.ZUp_YEast;
    157.                 break;
    158.             case Orientation.XUp_ZEast:
    159.                 _pivot = transform.position + new Vector3(0,-_scale.x/2.0f,-_scale.y/2.0f);
    160.                 _orientation = Orientation.YUp_ZEast;
    161.                 break;
    162.             case Orientation.YUp_XEast:
    163.                 _pivot = transform.position + new Vector3(0,-_scale.y/2.0f,-_scale.z/2.0f);
    164.                 _orientation = Orientation.ZUp_XEast;
    165.                 break;
    166.             case Orientation.YUp_ZEast:
    167.                 _pivot = transform.position + new Vector3(0,-_scale.y/2.0f,-_scale.x/2.0f);
    168.                 _orientation = Orientation.XUp_ZEast;
    169.                 break;
    170.             case Orientation.ZUp_YEast:
    171.                 _pivot = transform.position + new Vector3(0,-_scale.z/2.0f,-_scale.x/2.0f);
    172.                 _orientation = Orientation.XUp_YEast;
    173.                 break;
    174.             case Orientation.ZUp_XEast:
    175.                 _pivot = transform.position + new Vector3(0,-_scale.z/2.0f,-_scale.y/2.0f);
    176.                 _orientation = Orientation.YUp_XEast;
    177.                 break;
    178.             }
    179.             _axis = Vector3.right;
    180.             break;
    181.         }
    182.     }
    183. }
    184.  
     
  8. WeirdoBc

    WeirdoBc

    Joined:
    Dec 8, 2012
    Posts:
    20
    Trimmed down version.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public enum Direction {
    6.     North,
    7.     South,
    8.     East,
    9.     West
    10. }
    11.  
    12. public class Cuboid : MonoBehaviour {
    13.    
    14.     public float rotationSpeed = 250;
    15.    
    16.     private bool _moving;
    17.     private Direction _rotationDirection;
    18.     private Vector3 _pivot;
    19.     private float _totalRotation;
    20.     private Vector3 _axis;
    21.     private Vector3 _scale;
    22.  
    23.     void Start () {
    24.         _moving = false;
    25.         _scale = transform.localScale/2.0f;
    26.     }
    27.    
    28.     void Update () {
    29.         if (_moving){
    30.             float deltaRotation = rotationSpeed*Time.deltaTime;
    31.             if (_totalRotation + deltaRotation >= 90) {
    32.                 deltaRotation = 90 - _totalRotation;
    33.                 _moving = false;
    34.             }
    35.             if ((_rotationDirection == Direction.West)||(_rotationDirection == Direction.North))
    36.                 transform.RotateAround(_pivot,_axis, deltaRotation);
    37.             else transform.RotateAround(_pivot,_axis, -deltaRotation);
    38.            
    39.             _totalRotation += deltaRotation;
    40.         }
    41.         else if(Input.GetKeyUp(KeyCode.W)) Rotate(Direction.North);
    42.         else if(Input.GetKeyUp(KeyCode.A)) Rotate(Direction.West);
    43.         else if(Input.GetKeyUp(KeyCode.S)) Rotate(Direction.South);
    44.         else if(Input.GetKeyUp(KeyCode.D)) Rotate(Direction.East);
    45.  
    46.     }
    47.    
    48.     void Rotate(Direction direction){
    49.         _rotationDirection = direction;
    50.         _moving = true;
    51.         _totalRotation = 0;
    52.        
    53.         switch(_rotationDirection){
    54.         case Direction.East:
    55.             _pivot = transform.position + new Vector3(_scale.x,-_scale.y,0);
    56.             break;
    57.         case Direction.West:
    58.             _pivot = transform.position + new Vector3(-_scale.x,-_scale.y,0);
    59.             break;
    60.         case Direction.North:
    61.             _pivot = transform.position + new Vector3(0,-_scale.y,_scale.z);
    62.             break;
    63.         case Direction.South:
    64.             _pivot = transform.position + new Vector3(0,-_scale.y,-_scale.z);
    65.             break;
    66.         }
    67.        
    68.         if((_rotationDirection== Direction.East)||(_rotationDirection == Direction.West)){
    69.             _axis = Vector3.forward;
    70.             float temp = _scale.x;
    71.             _scale.x = _scale.y;
    72.             _scale.y = temp;
    73.         }else{
    74.             _axis = Vector3.right;
    75.             float temp = _scale.z;
    76.             _scale.z = _scale.y;
    77.             _scale.y = temp;
    78.         }
    79.     }
    80. }
    81.  
     
    arubassani likes this.