Hello, I am trying to make a game, with the movement behavior, like in the game Blockorz (See gameplay video), but I can’t make it roll properly. In the scripts I have used by searching this forum, I have made the cuboid to roll at the edge, but this is not working properly. I suspect its because I always use 0.5f as the half cube size, but the cuboid is a 1 width and 2 height cuboid.
Please help me, I have no clue how I can resolve this. The script I am using is this
using UnityEngine;
using System.Collections;
public class MoveCube : MonoBehaviour {
public float speed = 1.0f;
private Transform pivotPoint;
private float halfCubeSize = 1.0f;
private bool isRotating = false;
// Use this for initialization
void Awake () {
pivotPoint = (new GameObject("pivot")).transform;
}
// Update is called once per frame
void Update()
{
if (!isRotating)
{
if (Input.GetKey(KeyCode.RightArrow))
{
StartCoroutine(RotateCube(Vector3.right * halfCubeSize, -Vector3.forward));
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
StartCoroutine(RotateCube(-Vector3.right * halfCubeSize, Vector3.forward));
}
else if (Input.GetKey(KeyCode.UpArrow))
{
StartCoroutine(RotateCube(Vector3.forward * halfCubeSize, Vector3.right));
}
else if (Input.GetKey(KeyCode.DownArrow))
{
StartCoroutine(RotateCube(-Vector3.forward * halfCubeSize, -Vector3.right));
}
}
}
// Coroutine that rotates the cube on its bottom edges.
IEnumerator RotateCube(Vector3 refPoint, Vector3 rotationAxis)
{
isRotating = true;
pivotPoint.localRotation = Quaternion.identity;
pivotPoint.position = transform.position - Vector3.up * halfCubeSize + refPoint;
transform.parent = pivotPoint;
float angle = 0.0f;
while (angle < 90.0f)
{
angle += Time.deltaTime * 90.0f * speed;
pivotPoint.rotation = Quaternion.AngleAxis(Mathf.Min(angle, 90.0f), rotationAxis);
yield return null;
}
transform.parent = null;
isRotating = false;
yield return null;
}
}