- Home /
Flipping a cube while including the environment
I'm trying to emulate a cube-flipping motion similar to the gameplay of the game Edge. I found a code online which works great, but it unfortunately doesn't take the environment into consideration, due to the nature of the AlignBlock() function of the code.
My goal is to get the cube to make a check just before it "flops" for another cube in the way, or if it's on the edge of the floor. If there is another cube in the way, it should climb up on top of it, like in Edge, and climb down if it's on an edge.
I tried playing around with it, but I happen to be a beginner in unity coding, so I have no clue how to make the Quaternion.FromToRotation rotate correctly, which is where I suspect the problem is, though it could be somewhere else. Here is the code in question:
var speed: float = 2.5; // flops per second
var size: float = 1; // block size
var flopping = false;
function Update(){
var move = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (move.magnitude > 0.2) Flop(move);
}
function Flop(movDir: Vector2){
var pivot: Vector3;
var dir: Vector3;
if (flopping) return; // ignore other commands while flopping
flopping = true; // signals it's flopping
if (movDir.y > 0){ // move forward?
dir = Vector3.forward; // will flop forward
pivot = Vector3(0,-1,1); // defines point around which rotate
}
else
if (movDir.y < 0){
dir = -Vector3.forward;
pivot = Vector3(0,-1,-1);
}
else
if (movDir.x < 0){
dir = -Vector3.right;
pivot = Vector3(-1,-1,0);
}
else
if (movDir.x > 0){
dir = Vector3.right;
pivot = Vector3(1,-1,0);
}
AlignBlock(); // aligns block to grid before flopping
// calculates the point around which the block will flop
pivot = transform.position + (pivot * size / 2);
var org = transform.position - pivot;
var dest = (transform.position + dir * size) - pivot;
var rot0 = transform.rotation;
var rot1 = Quaternion.FromToRotation(org, dest) * rot0;
var a: float = 0;
while (a < 1){
var dt = Time.deltaTime * speed;
a += dt;
transform.position = Vector3.Slerp(org, dest, a) + pivot;
transform.rotation = Quaternion.Lerp(rot0, rot1, a);
yield;
}
if (audio) audio.Play(); // makes the flop sound
flopping = false;
}
private function AlignBlock(){
var angles = transform.eulerAngles;
// forces euler angles to be multiple of 90
angles.x = 90 * Mathf.RoundToInt(angles.x / 90);
angles.y = 90 * Mathf.RoundToInt(angles.y / 90);
angles.z = 90 * Mathf.RoundToInt(angles.z / 90);
transform.eulerAngles = angles;
var pos = transform.position;
// forces x and z to be in a grid
pos.x = size * Mathf.RoundToInt(pos.x / size);
pos.z = size * Mathf.RoundToInt(pos.z / size);
if (!rigidbody) pos.y = size * Mathf.RoundToInt(pos.y / size);
transform.position = pos;
}
Thanks in advance!
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Quick Angles Question 2 Answers
instantiating at different Angles? 1 Answer
Rotation along edge of a cube around another cube as its planet 1 Answer
Euler to Quaternion conversion returns unexpected result at exactly 180 degree rotation on Y axis 0 Answers