Search Unity

Speed changes

Discussion in 'Scripting' started by ralnur, Apr 2, 2009.

  1. ralnur

    ralnur

    Joined:
    Feb 9, 2009
    Posts:
    21
    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?
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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.)
     
  3. ralnur

    ralnur

    Joined:
    Feb 9, 2009
    Posts:
    21
    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:


    Code (csharp):
    1.     transform.position.z = currentZPosition; // we don't leave the z plane
    2.     rigidbody.velocity.z = 0.0;
    3.     transform.rotation = Quaternion.identity; // we never rotate
     
  4. ralnur

    ralnur

    Joined:
    Feb 9, 2009
    Posts:
    21
    Well, you might as well have all of the code in my Update function. I hope this will make it more clear.
    Code (csharp):
    1.  
    2. function Update () {
    3.     var h = Input.GetAxisRaw("Horizontal") * speed;
    4.     print(h);
    5.    
    6.     if (h == 0) {
    7.         isMoving = false;
    8.         rigidbody.velocity.x = h;// = 0
    9.     }
    10.     if (h != 0) {
    11.         isMoving = true;
    12.         rigidbody.velocity.x = h;
    13.     }
    14.  
    15.     var v = Input.GetAxisRaw("Vertical");
    16.     if (v > 0  !isJumping) {
    17.         isJumping = true;
    18.         rigidbody.velocity.y = v * jumpSpeed;
    19.     }
    20.     else if (v < 0  !isTouchingGround) {
    21.         rigidbody.velocity.y += v * jumpControl;
    22.     }
    23.    
    24.     if (Input.GetButtonDown("Jump")) {
    25.         GoToNextLevel();
    26.     }
    27.    
    28.     transform.position.z = currentZPosition; // we don't leave the z plane
    29.     rigidbody.velocity.z = 0.0;
    30.     transform.rotation = Quaternion.identity; // we never rotate the text
    31.    
    32.     collider.center = collider.transform.InverseTransformPoint(renderer.bounds.center);
    33.     collider.size = collider.transform.InverseTransformDirection(renderer.bounds.size);
    34.     collider.size = Vector3(collider.size.x, collider.size.y, .3); // give it some depth
    35. }
    [/code]
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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.
     
  6. ralnur

    ralnur

    Joined:
    Feb 9, 2009
    Posts:
    21
    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?
     
  7. ralnur

    ralnur

    Joined:
    Feb 9, 2009
    Posts:
    21
    (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.
     
  8. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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? :)
     
  9. ralnur

    ralnur

    Joined:
    Feb 9, 2009
    Posts:
    21
    (edit: fixed this)
     
  10. ralnur

    ralnur

    Joined:
    Feb 9, 2009
    Posts:
    21
    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.