While browsing the app store yesterday to find myself some new inspiration, I came across a game called "Hocus". It's a simple game that features a cube walking in the direction you slide your finger, and I found this to be quite fun. I ended up trying to code that sort of walking behaviour myself, but found it to be a lot harder than it looks. Any idea how I can effectively achieve walking like this? The closest I've gotten to the desired behaviour is something like this: Code (CSharp): private float x; void Update () { if (x < 90) { x++; } transform.rotation = Quaternion.Euler(x, 0, 0); } Which does turn it almost 90 degrees, but since it's a float, it ends up on 89.981. It also doesn't lift the cube as it walks. Is this actually a lot harder to achieve than it looks, or am I missing something?
a default cube has its origin in the center middle. To achieve that kind of "tumble" motion you will need to pivot around the edge of the cube that is in the direction of the movement. There are functions like https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html which might help with this.
Would it be a valid option to change the pivot point every 90 degrees? Cause RotateAround will achieve the desired effect for the first flip, but after that it'll just continue rotating around that point.
Could you give the cube a parent, perform the rotation in local space and then once it completes shift the cube back to the center and shift the parent forward by the same amount? Effectively resetting it but having it moved forward by one sides length. Then you could just repeat this function indefnitely in any direction as long as you use the correct axis in RotateAround. There may be a more clever solution, but this is the first one that came to my mind.
On second thought, just offsetting the axis in RotateAround based on your current position would be simpler.
had same topic here, didn't really come up with good solutions though.. https://forum.unity3d.com/threads/rotating-a-cube-around-different-pivots-suggestions.246436/
I've experimented a bit and here is the result: It's quicker to show the code than explaining it, so here it is: Code (CSharp): using UnityEngine; using System.Collections; public class Cube : MonoBehaviour { public float tumblingDuration = 0.2f; void Update() { var dir = Vector3.zero; if (Input.GetKey(KeyCode.UpArrow)) dir = Vector3.forward; if (Input.GetKey(KeyCode.DownArrow)) dir = Vector3.back; if (Input.GetKey(KeyCode.LeftArrow)) dir = Vector3.left; if (Input.GetKey(KeyCode.RightArrow)) dir = Vector3.right; if (dir != Vector3.zero && !isTumbling) { StartCoroutine(Tumble(dir)); } } bool isTumbling = false; IEnumerator Tumble(Vector3 direction) { isTumbling = true; var rotAxis = Vector3.Cross(Vector3.up, direction); var pivot = (transform.position + Vector3.down * 0.5f) + direction * 0.5f; var startRotation = transform.rotation; var endRotation = Quaternion.AngleAxis(90.0f, rotAxis) * startRotation; var startPosition = transform.position; var endPosition = transform.position + direction; var rotSpeed = 90.0f / tumblingDuration; var t = 0.0f; while (t < tumblingDuration) { t += Time.deltaTime; if( t < tumblingDuration) { transform.RotateAround(pivot, rotAxis, rotSpeed * Time.deltaTime); yield return null; } else { transform.rotation = endRotation; transform.position = endPosition; } } isTumbling = false; } }
only thing I would point out is that that code will only work on a flat plane, the original vid has a more complex environment with the cube tumbling across "walls" and the like; you might need to replace line 37 with something a little more in depth to check what plane it's rotating on as well.
I could explain how it works, if there are some lines that are not clear enough. It's essentially that:
creat script! I want to rotate the dice 3 or 5 times when i press the keys. But wenn I add *3 after the calling, like so: dir = Vector3.forward * 3; the dice jump directly 3 times in the direction. It will not rolling 3 times 1 side of the dice.. when i add a while or a fornext loop, nothing happens and the dice not rolling.. :-( Do you have a idea for the solution? sry for my bad english, but I hope you understand my question. THX