I'm working in a game type which the main scope is to roll a cube, i was searching methods to do this, and the best way which i could found is the method described by HiggyB in unity answers forum ( http://forum.unity3d.com/threads/16190-How-to-move-camera-automatically/page2). So let's Work!
Download de Project here, our create a cube and a javascript ,copy and paste the follow code and attach to the cube.
Select the cube and go to Menu GameObjec > Create Empty, rename it with the name targetpoint. In the Hierarchy tab, click and drag the new empty Object to the cube to make him your parent. Create a Javascript with the follow code and drag to targepoint object.
Dont forget to give values to the variables in the editor.
In the next post I intend to show how to load parameters from a xml file.
I hope you enjoy it, give your comments!
Download de Project here, our create a cube and a javascript ,copy and paste the follow code and attach to the cube.
private var ismoving : boolean = false; private var startY : float = 0; var cubeSpeed : float; var cubeSize: int; 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.033333); } // 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; }
Select the cube and go to Menu GameObjec > Create Empty, rename it with the name targetpoint. In the Hierarchy tab, click and drag the new empty Object to the cube to make him your parent. Create a Javascript with the follow code and drag to targepoint object.
var gizmocolor : Color; var Radius : float; function Update () { transform.rotation = Quaternion.identity; } function OnDrawGizmos(){ Gizmos.color = gizmocolor; Gizmos.DrawSphere (transform.position, Radius); }
Dont forget to give values to the variables in the editor.
In the next post I intend to show how to load parameters from a xml file.
I hope you enjoy it, give your comments!
nice tutorial
ReplyDeletethanks man
Hello, do you know if I can reuse this code but using forces instead of translations to move my cube and having axis constraints to stabilize the movement?
ReplyDeleteThanks
Great help tutorial.
ReplyDeleteBUT: it not works on Android:
Assets/Scripts/RollCube.js(37,35): BCE0051: Operator '*' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'.
Code:
var tSteps = Mathf.Ceil(aDuration* 30.0);
casting should b done.
Deletefunction DoRoll (aPoint, aAxis, aAngle: int, aDuration: float) {
var tSteps = Mathf.Ceil(aDuration * 30.0);
var tAngle = aAngle / tSteps;
if i change the dimension of the cube to (1*2*1) then the rotation changes a bit... can you check it
ReplyDeleteThere is a solution here : https://pastebin.com/zFZEMF55
DeleteYou have to create a quad for floor or mesh grill, and position the player ever your want;
Attach Rigidbody to take care of collision.
Hi, great tut,
ReplyDeleteHow can I change the code to make the roll automatically, because when I leave the if Condition out, the cube spins super faster and makes a cercle formation
Thanks
Hello, Creating a bool variable called automatic = true; for example.
Deleteand then a coroutine instead of the function
ienumerator Roll(...)
in the end of coroutine:
if(automatic)
startcoroutine(Roll(...));
Hi,
ReplyDeleteThank you very much for yout help it works now. I had actually made a Coroutine out of the function at first. But there is a point that I don’t understand. I removed the if statement to enable the Start of the couritine in the update function and this made the cube spinn completely wrong. Why does it work now, by putting an if statement which tells exactly the same thing (bool variable is always true)?
by starting the coroutine in the update function, you are calling the coroutine many times before this is complete finished. if you restart the coroutine at the end, you will execute once at time.
DeleteCan somebody please convert this script to c# ?
ReplyDeletehttp://forum.unity3d.com/threads/issues-with-converting-java-roll-a-cube-script-to-c.329313/ Help please!!!
DeleteI need in a C#. Please help me!
ReplyDelete