Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Rolling rigidbody cube in Unity3D

$
0
0
So in Unity3D I have a cube which is being controlled by a player(using arrows). Player can go wherever he wants with the cube. The problem is when I add rigidbody to the cube then it doesn't really go that smooth anymore, somehow at the end it just glitches. Tried to play a bit with `torque`, but that did not help or I do not understand yet clearly how to implement it correctly. This is the code I am using for the cube (it is written in JavaScript). Any help will be appreciated, even if you do not know the javascript an idea still counts. Thank you. Code : private var ismoving : boolean = false; private var startY : float = 0; var cubeSpeed : float; var cubeSize : float; var jumpHeight = 3.0; 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)); } if (Input.GetKeyDown("space") && ismoving == false) { GetComponent.().AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse); } } function DoRoll (aPoint, aAxis, aAngle, aDuration) { var tSteps = Mathf.Ceil(aDuration * 30.0); var tAngle = aAngle / tSteps; var pos : Vector3; for (var i = 1; i <= tSteps; i++) { transform.RotateAround (aPoint, aAxis, tAngle); yield WaitForSeconds(0.0033333); } transform.Find("targetpoint").position = transform.position; pos = transform.position; pos.y = startY; transform.position = pos; 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; ismoving = false; } function FixedUpdate () { }

Viewing all articles
Browse latest Browse all 1875

Trending Articles