I am working on a 3d demo. In that, I have got plane which is a tiled map. I have cube on that plane. I want to move cube from one tile to another. The movement of cube should be like its getting flipped on its edge.
I was able to solve this problem by creating empty game object and putting it at the center of the cube and then 4 more empty game on each edge of bottom face.
So my object structure is like
–player
–centerObject
–leftPivot
–rightPivot
–FrontPivot
–RearPivot
Reason for not making centerObject a child of player object is that, whenever player flips its axes changes too and now if centerObject was child of payer object then it will mean that centerObject will also get rotated by it. That is why I have kept it as seperate object and I update its position as player object moves.
then in code of player object
//whenever original player moves then update centerObject's position
player.transform.position = gameObject.transform.position;
GameObject child;
if (Input.GetKeyDown(KeyCode.UpArrow)){
child = GameObject.Find("FrontPivot");
gameObject.transform.RotateAround(child.transform.position, Vector3.left, -90);
}
if (Input.GetKeyDown(KeyCode.DownArrow)){
child = GameObject.Find("RearPivot");
gameObject.transform.RotateAround(child.transform.position, Vector3.left, 90);
}
if (Input.GetKeyDown(KeyCode.LeftArrow)){
child = GameObject.Find("LeftPivot");
gameObject.transform.RotateAround(child.transform.position, Vector3.forward, 90);
}
if (Input.GetKeyDown(KeyCode.RightArrow)){
child = GameObject.Find("RightPivot");
gameObject.transform.RotateAround(child.transform.position, Vector3.forward, -90);
}