- Home /
Make ball flip around cube...
Imagine a 1x1x1-cube. On top of that place a sphere with diameter 1. I would like to able to roll this ball around the sides of the cube. Ie. when you press "right", the ball will roll to the edge (ie. 0.5 units) and then rotate around the edge (90 degrees), and go down to the middle (again 0.5 units). In that way in ends at the right side of the cube. Press "right" again will roll the ball to the lower side of the cube. And so forth.
Of course I would also like to to be able to press "up" at any point and roll forward, and so forth.
I am very new to Unity so I have no idea how to do this in a smart way. I have tried manually coding it, but the code becomes horrible, and furthermore, it's not even working properly. I think, somehow, I misunderstand how the local axis rotate.
YOU DO NOT NEED TO READ THE FOLLOWING SCRIPT, a good answer hopefully does not include my code :) - but, the code does almost work, so if someone would try and open Unity, create a cube at (0,0,0) with scale(1,1,1), and a cube at (0,1,0), rotation (0, 90, 0), and scale (1,1,1), and apply this code to the sphere, you can see what I mean...
The code is horrible though, I know. Especially the way I have to correct for minor rotation mistakes.
ANY advice will be highly appreciated.
#pragma strict
static var doingRoll : boolean = false;
private var speed : float = 10.0;
private var radius : float = 0.5;
private var angularScale : float = 180 / (Mathf.PI * radius) ;
private var moved : float = 0;
private var rotated : float = 0;
private var moved2 : float = 0;
private var corner : Vector3;
private var finalPos : Vector3;
private var finalRot : Vector3;
private var moveVec : Vector3;
private var axis : String;
function Update () {
if (Input.GetKeyDown("d") && !doingRoll) {
doingRoll = true;
axis = "right";
initRoll();
} else if (Input.GetKeyDown("a") && !doingRoll) {
doingRoll = true;
axis = "left";
initRoll();
} else if (Input.GetKeyDown("w") && !doingRoll) {
doingRoll = true;
axis = "forward";
initRoll();
}
if (doingRoll) {
roll();
}
}
function initRoll() {
moved = 0;
rotated = 0;
moved2 = 0;
finalRot = transform.eulerAngles;
if (axis == "right") {
corner = transform.position + radius * (transform.right - transform.up);
finalPos = transform.position + 2 * radius * (transform.right - transform.up);
finalRot.z = finalRot.z - 90;
moveVec = transform.forward;
} else if (axis == "left") {
corner = transform.position + radius * (-transform.right - transform.up);
finalPos = transform.position + 2 * radius * (-transform.right - transform.up);
finalRot.z = finalRot.z + 90;
moveVec = -transform.forward;
} else if (axis == "forward") {
corner = transform.position + radius * (transform.forward - transform.up);
finalPos = transform.position + 2 * radius * (transform.forward- transform.up);
finalRot.x = finalRot.x + 90;
moveVec = -transform.right;
}
}
function roll() {
if (moved < radius) {
transform.Translate(moveVec * speed * Time.deltaTime);
moved += speed * Time.deltaTime;
return;
} else if (rotated < 90) { // 90 degrees
transform.RotateAround (corner, moveVec, - speed * angularScale * Time.deltaTime);
rotated += speed * angularScale * Time.deltaTime;
return;
} else if (moved2 < radius) {
transform.Translate(moveVec * speed * Time.deltaTime);
moved2 += speed * Time.deltaTime;
return;
} else {
doingRoll = false;
transform.position = finalPos;
transform.eulerAngles = finalRot;
return;
}
}