- Home /
Rectangular Cube is sinking in floor rotating at 90 degree
Okay so I am trying to make a game like "bloxorz" in which a rectangular cube is rotated at 90 degrees. But when I try to implement it the cube is half sinking to the floor at 90 and then fully sink at another 90 degree. And I am using box collider and script that is given below. And I also attached an empty gameobject to parent cube. I am thinking that rigid body will help but I am new to unity so don't know how to implement it. And also that cube is working perfectly but only rectangular cube has this problem. Please help I am stuck at this.
Code for Rectangular Cube:
private var ismoving : boolean = false;
private var startY : float = 0;
var cubeSpeed : float;
var cubeSize : float;
function Update ()
{
if (Input.GetKeyDown("up") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(0, -cubeSize/2 , cubeSize/2);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.right, 90.0f,cubeSpeed));
}
if (Input.GetKeyDown("down") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(0, -cubeSize/2, -cubeSize/2);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.right, 90.0f,cubeSpeed));
}
if (Input.GetKeyDown("left") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(-cubeSize/2, -cubeSize/2, 0);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.forward, 90.0,cubeSpeed));
}
if (Input.GetKeyDown("right") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(cubeSize/2, -cubeSize/2, 0);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.forward, 90.0f,cubeSpeed));
}
}
function DoRoll (aPoint, aAxis, aAngle, aDuration) {
var tSteps = Mathf.Ceil(aDuration * 30.0);
var tAngle = aAngle / tSteps;
var pos : Vector3; // declare variable to fix the y position
// Rotate the cube by the point, axis and angle
for (var i = 1; i <= tSteps; i++)
{
transform.RotateAround (aPoint, aAxis, tAngle);
yield WaitForSeconds(0.0033333);
Debug.Log("Tsteps : " + tSteps);
}
// move the targetpoint to the center of the cube
transform.Find("targetpoint").position = transform.position;
// Make sure the y position is correct
pos = transform.position;
pos.y = startY;
transform.position = pos;
// Make sure the angles are snaping to 90 degrees.
var vec = transform.eulerAngles;
vec.x = Mathf.Round(vec.x / 90) * 90;
vec.y = Mathf.Round(vec.y / 90) * 90;
vec.z = Mathf.Round(vec.z / 90) * 90;
transform.eulerAngles = vec;
// The cube is stoped
ismoving = false;
}
Code for child GameObject:
var Radius : float;
function Update () {
transform.rotation = Quaternion.identity;
}
function OnDrawGizmos(){
Gizmos.DrawSphere (transform.position, Radius);
}
If you put this in Update() or FixedUpdate()?
// $$anonymous$$ake sure the y position is correct
pos = transform.position;
pos.y = startY;
transform.position = pos;
Because inside DoRoll() you are calling this correction only once.
Thanks for the reply Nido. I tried that and put it in Update() but then also no success. And even when I rotate it in inspector then also it is half sunk in floor at 90 degree.
Hm... then without seeing how it's behaving I only may suggest to use a parent who will move arround and the cube controlling the rotation (use animation maybe?). Or moving the cube's pivot to the edge that will stay in the floor during the rotation. The second looks more accurated for your problem :)
Okay I am uploading pics so u guy can understand what I am saying.
okay see edges at cube is rotating. 1st step cube is at 90 degree and another is at 180 degree. Now after 1st step it should rotate using c edge but ins$$anonymous$$d it rotate using b edge and turns 360 degree ins$$anonymous$$d of 180 degree. And u can see that is half sink in floor in 1st pic and full sink in 2nd pic.
Your answer
Follow this Question
Related Questions
Ball getting stuck in surfaces and bouncing for no reason? Help! 0 Answers
Rigidbody.Addforce not working in Unity 5.4.1 3 Answers
Help with rigidbodies and follow transform 1 Answer
Rigidbody.MovePosition doesn't move reliably? 1 Answer
Unity hinge joint jitter when target gameObject parent is moving 1 Answer