I was wondering if anybody had scripts for rotating cuboids, something like this: http://www.youtube.com/watch?v=bheCWlrtp-o Any body know of or have a script?
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
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
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
Did it! Easier than I thought, when you use the Transform.RotateAround function. Code (csharp): using UnityEngine; using System.Collections; public enum Direction { North, South, East, West, Up } public class Cuboid : MonoBehaviour { public float rotationSpeed = 200; private Direction _topDirection; private bool _moving; private Direction _rotationDirection; private Vector3 _pivot; private float _totalRotation; private Vector3 _axis; void Start () { _moving = false; _topDirection = Direction.Up; } void Update () { if (_moving){ float deltaRotation = rotationSpeed*Time.deltaTime; if (_totalRotation + deltaRotation >= 90) { deltaRotation = 90 - _totalRotation; _moving = false; } if ((_rotationDirection == Direction.West)||(_rotationDirection == Direction.North)) transform.RotateAround(_pivot,_axis, deltaRotation); else transform.RotateAround(_pivot,_axis, -deltaRotation); _totalRotation += deltaRotation; } else if(Input.GetKeyUp(KeyCode.W)) Rotate(Direction.North); else if(Input.GetKeyUp(KeyCode.A)) Rotate(Direction.West); else if(Input.GetKeyUp(KeyCode.S)) Rotate(Direction.South); else if(Input.GetKeyUp(KeyCode.D)) Rotate(Direction.East); } void Rotate(Direction direction){ _rotationDirection = direction; _moving = true; _totalRotation = 0; switch(_rotationDirection){ case Direction.East: if((_topDirection == Direction.East)||(_topDirection == Direction.West)){ _topDirection = Direction.Up; _pivot = transform.position + new Vector3(1,-.5f,0); }else if(_topDirection == Direction.Up){ _topDirection = Direction.East; _pivot = transform.position + new Vector3(.5f,-1,0); }else _pivot = transform.position + new Vector3(.5f,-.5f,0); _axis = Vector3.forward; break; case Direction.West: if((_topDirection == Direction.East)||(_topDirection == Direction.West)){ _topDirection = Direction.Up; _pivot = transform.position + new Vector3(-1,-.5f,0); }else if(_topDirection == Direction.Up){ _topDirection = Direction.West; _pivot = transform.position + new Vector3(-.5f,-1,0); }else _pivot = transform.position + new Vector3(-.5f,-.5f,0); _axis = Vector3.forward; break; case Direction.North: if((_topDirection == Direction.North)||(_topDirection == Direction.South)){ _topDirection = Direction.Up; _pivot = transform.position + new Vector3(0,-.5f,1); }else if(_topDirection == Direction.Up){ _topDirection = Direction.North; _pivot = transform.position + new Vector3(0,-1,.5f); }else _pivot = transform.position + new Vector3(0,-.5f,.5f); _axis = Vector3.right; break; case Direction.South: if((_topDirection == Direction.North)||(_topDirection == Direction.South)){ _topDirection = Direction.Up; _pivot = transform.position + new Vector3(0,-.5f,-1); }else if(_topDirection == Direction.Up){ _topDirection = Direction.South; _pivot = transform.position + new Vector3(0,-1,-.5f); }else _pivot = transform.position + new Vector3(0,-.5f,-.5f); _axis = Vector3.right; break; } } } Not bad for a begginer, heh? mata ne, WeirdoBc
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
Try this new version, it works with any scale of cube, like, exemple, x=3, y=5, z=2: Code (csharp): using UnityEngine; using System.Collections; public enum Direction { North, South, East, West } public enum Orientation { YUp_XEast, YUp_ZEast, XUp_YEast, XUp_ZEast, ZUp_XEast, ZUp_YEast } public class Cuboid : MonoBehaviour { public float rotationSpeed = 250; private Orientation _orientation; private bool _moving; private Direction _rotationDirection; private Vector3 _pivot; private float _totalRotation; private Vector3 _axis; private Vector3 _scale; void Start () { _moving = false; _orientation = Orientation.YUp_XEast; _scale = transform.localScale; } void Update () { if (_moving){ float deltaRotation = rotationSpeed*Time.deltaTime; if (_totalRotation + deltaRotation >= 90) { deltaRotation = 90 - _totalRotation; _moving = false; } if ((_rotationDirection == Direction.West)||(_rotationDirection == Direction.North)) transform.RotateAround(_pivot,_axis, deltaRotation); else transform.RotateAround(_pivot,_axis, -deltaRotation); _totalRotation += deltaRotation; } else if(Input.GetKeyUp(KeyCode.W)) Rotate(Direction.North); else if(Input.GetKeyUp(KeyCode.A)) Rotate(Direction.West); else if(Input.GetKeyUp(KeyCode.S)) Rotate(Direction.South); else if(Input.GetKeyUp(KeyCode.D)) Rotate(Direction.East); } void Rotate(Direction direction){ _rotationDirection = direction; _moving = true; _totalRotation = 0; switch(_rotationDirection){ case Direction.East: switch(_orientation){ case Orientation.XUp_YEast: _pivot = transform.position + new Vector3(_scale.y/2.0f,-_scale.x/2.0f,0); _orientation = Orientation.YUp_XEast; break; case Orientation.XUp_ZEast: _pivot = transform.position + new Vector3(_scale.z/2.0f,-_scale.x/2.0f,0); _orientation = Orientation.ZUp_XEast; break; case Orientation.YUp_XEast: _pivot = transform.position + new Vector3(_scale.x/2.0f,-_scale.y/2.0f,0); _orientation = Orientation.XUp_YEast; break; case Orientation.YUp_ZEast: _pivot = transform.position + new Vector3(_scale.z/2.0f,-_scale.y/2.0f,0); _orientation = Orientation.ZUp_YEast; break; case Orientation.ZUp_YEast: _pivot = transform.position + new Vector3(_scale.y/2.0f,-_scale.z/2.0f,0); _orientation = Orientation.YUp_ZEast; break; case Orientation.ZUp_XEast: _pivot = transform.position + new Vector3(_scale.x/2.0f,-_scale.z/2.0f,0); _orientation = Orientation.XUp_ZEast; break; } _axis = Vector3.forward; break; case Direction.West: switch(_orientation){ case Orientation.XUp_YEast: _pivot = transform.position + new Vector3(-_scale.y/2.0f,-_scale.x/2.0f,0); _orientation = Orientation.YUp_XEast; break; case Orientation.XUp_ZEast: _pivot = transform.position + new Vector3(-_scale.z/2.0f,-_scale.x/2.0f,0); _orientation = Orientation.ZUp_XEast; break; case Orientation.YUp_XEast: _pivot = transform.position + new Vector3(-_scale.x/2.0f,-_scale.y/2.0f,0); _orientation = Orientation.XUp_YEast; break; case Orientation.YUp_ZEast: _pivot = transform.position + new Vector3(-_scale.z/2.0f,-_scale.y/2.0f,0); _orientation = Orientation.ZUp_YEast; break; case Orientation.ZUp_YEast: _pivot = transform.position + new Vector3(-_scale.y/2.0f,-_scale.z/2.0f,0); _orientation = Orientation.YUp_ZEast; break; case Orientation.ZUp_XEast: _pivot = transform.position + new Vector3(-_scale.x/2.0f,-_scale.z/2.0f,0); _orientation = Orientation.XUp_ZEast; break; } _axis = Vector3.forward; break; case Direction.North: switch(_orientation){ case Orientation.XUp_YEast: _pivot = transform.position + new Vector3(0,-_scale.x/2.0f,_scale.z/2.0f); _orientation = Orientation.ZUp_YEast; break; case Orientation.XUp_ZEast: _pivot = transform.position + new Vector3(0,-_scale.x/2.0f,_scale.y/2.0f); _orientation = Orientation.YUp_ZEast; break; case Orientation.YUp_XEast: _pivot = transform.position + new Vector3(0,-_scale.y/2.0f,_scale.z/2.0f); _orientation = Orientation.ZUp_XEast; break; case Orientation.YUp_ZEast: _pivot = transform.position + new Vector3(0,-_scale.y/2.0f,_scale.x/2.0f); _orientation = Orientation.XUp_ZEast; break; case Orientation.ZUp_YEast: _pivot = transform.position + new Vector3(0,-_scale.z/2.0f,_scale.x/2.0f); _orientation = Orientation.XUp_YEast; break; case Orientation.ZUp_XEast: _pivot = transform.position + new Vector3(0,-_scale.z/2.0f,_scale.y/2.0f); _orientation = Orientation.YUp_XEast; break; } _axis = Vector3.right; break; case Direction.South: switch(_orientation){ case Orientation.XUp_YEast: _pivot = transform.position + new Vector3(0,-_scale.x/2.0f,-_scale.z/2.0f); _orientation = Orientation.ZUp_YEast; break; case Orientation.XUp_ZEast: _pivot = transform.position + new Vector3(0,-_scale.x/2.0f,-_scale.y/2.0f); _orientation = Orientation.YUp_ZEast; break; case Orientation.YUp_XEast: _pivot = transform.position + new Vector3(0,-_scale.y/2.0f,-_scale.z/2.0f); _orientation = Orientation.ZUp_XEast; break; case Orientation.YUp_ZEast: _pivot = transform.position + new Vector3(0,-_scale.y/2.0f,-_scale.x/2.0f); _orientation = Orientation.XUp_ZEast; break; case Orientation.ZUp_YEast: _pivot = transform.position + new Vector3(0,-_scale.z/2.0f,-_scale.x/2.0f); _orientation = Orientation.XUp_YEast; break; case Orientation.ZUp_XEast: _pivot = transform.position + new Vector3(0,-_scale.z/2.0f,-_scale.y/2.0f); _orientation = Orientation.YUp_XEast; break; } _axis = Vector3.right; break; } } }
Trimmed down version. Code (csharp): using UnityEngine; using System.Collections; public enum Direction { North, South, East, West } public class Cuboid : MonoBehaviour { public float rotationSpeed = 250; private bool _moving; private Direction _rotationDirection; private Vector3 _pivot; private float _totalRotation; private Vector3 _axis; private Vector3 _scale; void Start () { _moving = false; _scale = transform.localScale/2.0f; } void Update () { if (_moving){ float deltaRotation = rotationSpeed*Time.deltaTime; if (_totalRotation + deltaRotation >= 90) { deltaRotation = 90 - _totalRotation; _moving = false; } if ((_rotationDirection == Direction.West)||(_rotationDirection == Direction.North)) transform.RotateAround(_pivot,_axis, deltaRotation); else transform.RotateAround(_pivot,_axis, -deltaRotation); _totalRotation += deltaRotation; } else if(Input.GetKeyUp(KeyCode.W)) Rotate(Direction.North); else if(Input.GetKeyUp(KeyCode.A)) Rotate(Direction.West); else if(Input.GetKeyUp(KeyCode.S)) Rotate(Direction.South); else if(Input.GetKeyUp(KeyCode.D)) Rotate(Direction.East); } void Rotate(Direction direction){ _rotationDirection = direction; _moving = true; _totalRotation = 0; switch(_rotationDirection){ case Direction.East: _pivot = transform.position + new Vector3(_scale.x,-_scale.y,0); break; case Direction.West: _pivot = transform.position + new Vector3(-_scale.x,-_scale.y,0); break; case Direction.North: _pivot = transform.position + new Vector3(0,-_scale.y,_scale.z); break; case Direction.South: _pivot = transform.position + new Vector3(0,-_scale.y,-_scale.z); break; } if((_rotationDirection== Direction.East)||(_rotationDirection == Direction.West)){ _axis = Vector3.forward; float temp = _scale.x; _scale.x = _scale.y; _scale.y = temp; }else{ _axis = Vector3.right; float temp = _scale.z; _scale.z = _scale.y; _scale.y = temp; } } }