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.<Rigidbody>().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 () {
}
It's likely not moving smoothly because you are modifying its position and rotation directly
with its transform instead of using Rigidbody
functions.
Once Rigidbody
is attached to it, don't use transform.position = pos;
or transform.eulerAngles = vec
;
Rigidbody.MovePosition
, Rigidbody.MoveRotation
, Rigidbody.AddForce
, and Rigidbody.AddTorque
are the functions you should be using.
Replace these in your DoRoll
function
Rigidbody rb;
rb = GetComponent<Rigidbody>();
rb.MovePosition(pos);
rb.MoveRotation(Quaternion.Euler(vec));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.