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 https://www.assetstore.unity3d.com/en/#!/content/17978
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?
Answer by AJNK · Aug 28, 2015 at 06:37 PM
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);
}
Answer by thomasfriday · Jul 10, 2021 at 03:34 PM
Here's a short Youtube video that covers exactly how to roll a cube on its edges: https://youtu.be/06rs3U2bpy8