How to flip cube on its edge?

Hi,

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.

Same as in this game Unity Asset Store - The Best Assets for Game Making

can you guys help me with code? I dont know how to implement it.

Problems that I am facing is,

  1. How to know which direction is forward after cube is rolled in any direction?
  2. How to roll cube so that it looks like its flipped on 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);
		}

Here’s a short Youtube video that covers exactly how to roll a cube on its edges: How to Move a Cube by Rolling it | Unity Tutorial - YouTube


rolling cube