Search Unity

Problems with directly setting Tranform.position

Discussion in 'Physics' started by abopo280, Jan 27, 2015.

  1. abopo280

    abopo280

    Joined:
    Jan 12, 2014
    Posts:
    4
    I'm making a 2D platformer that uses a rigidbody2D and a bunch of physics for the player movement. Recently I have been attempting to ground clamp the player object so it won't sail off curved surfaces at high speeds. The technique I've been using is simple: sending a raycast below the player, and if it hits something, set its position to slightly above the collision point.

    For some reason, doing this causes the movement speed of the player to slow down to about half of what it was, along with some visible jittering as the player walks forward.

    Just for fun, while trying to fix this I replaced the line that sets the player's position to just simply "transform.position = transform.position;" and somehow the problem STILL occurs. I am at a total loss at this point as I cannot understand how setting the position to itself could affect anything. I double checked and made sure that the values being used were the same and they were.

    I can only imagine there is some behind-the-scenes stuff Unity runs when the position gets changed. Does anyone have any idea how/why this could happen?
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Are you doing that raycasting in an Update() function? Are you caching the transform? Post your code is probably the best bet.
     
  3. abopo280

    abopo280

    Joined:
    Jan 12, 2014
    Posts:
    4
    The raycasting is being done in a FixedUpdate() function. I am not caching the transform. It's probably going to be difficult to isolate the problem in code, but I'll see what I can do.

    Also I'm more interested in the setting position to itself issue than the actual ground clamping. I feel like if I solve the former I'll be able to solve the latter.
     
  4. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    I think that raycasting, and to a lesser degree the constant referencing of transform, are probably causing your problems.

    Also, why would setting transform.position = transform.position have any effect on... anything? I'm not sure where you're going with that.
     
  5. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    you might as well completely remove the line with transform.position = transform.position, it just says "change nothing" which isn't useful for any testing purposes.

    you don't need to keep raycasts in fixedupdate; in fact, it might be better to move that into a regular update function.

    what axis is the jittering occuring on, the Y axis, the X axis, or both? i'm guessing it's the Y axis. in that case, do you have any gravity going on? does the player have a rigidbody2D component? if so, gravity may be affecting it. in which case the problem is being caused by having your raycast in FixedUpdate(), which doesn't occur every frame; therefor, as regular frames pass by, your player is being pulled by gravity, falling a bit, then performing the raycast on a physics frame, adjusting its position accordingly, and in the next frame it starts falling again.

    i don't know if my assumptions are correct but try putting it in Update() and let us know what happens.
     
  6. abopo280

    abopo280

    Joined:
    Jan 12, 2014
    Posts:
    4
    I have no idea why setting transform.position = transform.position would have an effect on anything, but it is. That's my question.

    To simplify things: ignore the raycasting stuff I mentioned. If I remove all that and just put transform.position = transform.position directly into the player's update function, the problem occurs; without that line, it doesn't. My best guess is there's some kind of back-end stuff Unity does when the position is changed that is affecting something.

    At this point I'm more interested is this weird occurrence than fixing my ground clamping. I just want to figure out what is happening.
     
  7. abopo280

    abopo280

    Joined:
    Jan 12, 2014
    Posts:
    4
    Ok I've figured out what is causing the weird effect: it's the Interpolate variable on the rigidbody. If I set it to "None" instead of "Interpolate" the effect goes away. I assume that when the rigidbody interpolates it saves any changes made to the transforms position and applies them over time. That's why setting the position to itself made the object slow down and jitter, because the object would interpolate forward based on velocity, and then interpolate backwards to when it was set to itself a couple frames ago.
     
  8. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Yep - sorry. I'd misread you :)