# Speed changes

I have a rigidbody on a box collider, and when I set its velocity I have problems. The code looks something like this

var h = Input.GetAxisRaw(“Horizontal”) * speed;

if(h != 0) {
rigidbody.velocity.x = h;
}

When I start the scene (without changing the “speed” variable), I get different speeds each time! Often it is the same, but sometimes it is really slow, sometimes a faster speed (which is what I think it should be normally), sometimes inbetween.

Any ideas?

Where is the above code located? Update? FixedUpdate? Other? What values of h are you getting? Is there anything else that might cause the body to move? (minor collisions, gravity, etc.)

The code is located in Update.

Values of h are -30 to 30, when I am using speed = 30. I have played around with this variable, and I get h = +/- whatever speed I use.

There are other things that will cause the body to move. It is standing on a platform, and there is gravity. Perhaps where the error lies is in this: I am confining motion to a 2D plane. I want the body to not move out of it. So at the end of the Update function I have this code:

``````	transform.position.z = currentZPosition; // we don't leave the z plane
rigidbody.velocity.z = 0.0;
transform.rotation = Quaternion.identity; // we never rotate
``````

Well, you might as well have all of the code in my Update function. I hope this will make it more clear.

``````function Update () {
var h = Input.GetAxisRaw("Horizontal") * speed;
print(h);

if (h == 0) {
isMoving = false;
rigidbody.velocity.x = h;// = 0
}
if (h != 0) {
isMoving = true;
rigidbody.velocity.x = h;
}

var v = Input.GetAxisRaw("Vertical");
if (v > 0  !isJumping) {
isJumping = true;
rigidbody.velocity.y = v * jumpSpeed;
}
else if (v < 0  !isTouchingGround) {
rigidbody.velocity.y += v * jumpControl;
}

if (Input.GetButtonDown("Jump")) {
GoToNextLevel();
}

transform.position.z = currentZPosition; // we don't leave the z plane
rigidbody.velocity.z = 0.0;
transform.rotation = Quaternion.identity; // we never rotate the text

collider.center = collider.transform.InverseTransformPoint(renderer.bounds.center);
collider.size = collider.transform.InverseTransformDirection(renderer.bounds.size);
collider.size = Vector3(collider.size.x, collider.size.y, .3); // give it some depth
}
``````

[/code]

Step 1: don’t use Update, use FixedUpdate instead.

With that out of the way, I’ll see about digesting your code but please try the above ASAP and see what changes, if any, it brings about.

The bug is slightly irregular/not predictable, but it appears as though this has fixed the irregularity in speed.

However, now I experience much more “wobbling” of the body. I’m assuming this is because the rotation is not being set to the identity quaternion as often now. Any advice?

(edit: the wobbling doesn’t seem to have to do with the resizing of the collider, I commented out that code and the behavior was the same.)

When I hit the right arrow the box stars moving right, but continually hits the floor and twitches (quite large twitches too). I would like the box to just slide along the platform, without freaking out.

Can you post a sample project? I’m wondering about whether it’s wise to set the x, y and z velocity components separately and whether you ought to be manually forcing a z-value in place. I’d like to see your set-up in action and go from there. Possible?

(edit: fixed this)

Holy crap.

I just realized rigidbody has a “freezeRotation” variable.

Well, that fixes a lot of things. Gah.

http://www.mediafire.com/?qhjkyzdwloy

(^ I am updating this link as much as possible.)

1. If you go to the right and the jump on top of the little cube, the collision detection doesn’t seem to work. The text just goes right through the cube.