Move an object around all 6 sides of a cube
For the past couple of days I've been experimenting with different ways to move one smaller cube around a big cube. Each method I've tried seems to work but not perfectly as there are bugs or other problems with the solution. I'll attach a few photos to show you what I'm trying to achieve. It's a 3D snake game where the player moves around a cube.
I've tried three different things each with not the best results. So to start off with I'm just using a basic movement script to move the cube around.
void playerMovement(){
player.transform.Translate (Vector3.forward * (Time.deltaTime * speed));
if (Input.GetKeyDown ("left")) {
player.transform.Rotate (0, -90, 0);
}
if (Input.GetKeyDown ("right")) {
player.transform.Rotate (0, 90, 0);
}
}
So the first thing I tried was using box colliders with triggers set on the edges to rotate the player 90 degrees. This presented two problems. The first problem being that I needed two triggers on each edge and this would make the player cube rotate inside in the bigger cube. So I added a wait timer before you could rotate again. This worked but now if the player moves around the edges and stays in contact with the cube once he exits either side it will rotate the player cube inside the bigger cube again.
IEnumerator waitForExit(){
yield return new WaitForSeconds (1.5f);
player.GetComponent<movement>().canRotate = true;
}
void OnTriggerExit(Collider other) {
if (player.GetComponent<movement>().canRotate == true) {
other.gameObject.transform.Rotate (90, 0, 0);
player.GetComponent<movement>().canRotate = false;
player.GetComponent<movement> ().disableTurn = false;
StartCoroutine (waitForExit ());
}
}
So the last thing I tried was using a downwards force on the cube that would make the cube stick to the surface no matter what side it was on. But this presented an issue where when the cube tipped over the edge it would bounce and throw off the rotation slightly. Even with a freezed rotation it would still give the same result.
void FixedUpdate(){
rb.AddForce(-transform.up * gravity);
}
So this is where I'm stuck at. I'm still trying to find the best method to achieve the desired results. I've looked at methods that use gravity on spherical objects but this doesn't seem like what I need either. I've looked into a pivot point method or raycasting but wouldn't know where to start with those. Any methods or a fix to methods above which you can think of that would help would be greatly appreciated.
here is another idea. you know the dimensions of the cube exactly. that means, you know if the next step goes off edge or not. that means, with a little vector math you should be able to turn your cube in an instant around the edges. using the dot product you could even split the moment into rotation around the edge axis and moment along the axis to make the character rotate amount the edges. this will get hard on the outer corners though.
Thanks hexagonius for the idea. Using your suggested idea I clamped the X, Y and Z values of the player cube transform to the dimensions of the bigger cube. Once the player cube reached lowest or highest clamped values I rotated the player cube 90 degrees. I'll take a look into using a dot product as the idea of a smooth rotation sounds nice :)
Answer by PSpiroz · Mar 16, 2017 at 10:44 AM
???
Did not quit get the original problem. I think you expose us your solution's problems.