Search Unity

2D physics - problem with interpolation and contact points

Discussion in '2D' started by harrynesbitt, Nov 14, 2013.

  1. harrynesbitt

    harrynesbitt

    Joined:
    Feb 11, 2013
    Posts:
    14
    Hi guys,

    I'm incredibly impressed with the new 2D tools, but in the process of switching to the new physics I've come across a couple of strange problems.

    1) Rigidbody2D interpolation

    While my player is jumping, I disable isKinematic, apply a force, and let physics do the work. Under the 3D system, interpolation works great for smoothing out motion between physics steps. Using the 2D Rigidbody with the same settings gives me very strange results -

    No matter which interpolation mode I use (interpolate, extrapolate, none) I still get choppy motion. Stranger still is that using interpolate causes the player to travel slower through space (like it's moving through treacle) and cover less ground, while using extrapolate causes the player to travel faster. The effect is unrelated to the initial jump force or gravity, but it is affected by changing the Fixed Timestep under Project Settings > Time - setting a higher value exaggerates the slow-down/speed-up but even with a really low timestep of 0.001 it's still noticeable.

    Increasing the velocity and position iterations under Project Settings > Physics 2D also has no affect on either the choppiness or the slow motion.

    2) Collision contact points

    The second problem occurs when using OnCollisionEnter2D() to detect when the player makes contact with the terrain surface after landing a jump. The player has a CircleCollider2D, and the terrain surface is an EdgeCollider2D, created dynamically at runtime.

    However, the collision event is being called during the jump, way before any contact is actually made. Debugging the contact points, I can see the collision occurring away from the player's current position (further along the terrain, where the player has previously been).

    The image below shows the player and it's CircleCollider2D moving from left to right - I've paused it after jumping, on the frame where OnCollisionEnter2D() is called. The blue cross is drawn at the contact point (ignore the red / green lines - they're for visualising a raycast) and the terrain surface is the wavy green line -

    $collision.png

    Has anyone come across similar issues, or have any ideas what could be causing them?
     
    Last edited: Nov 14, 2013
    emiru84 likes this.
  2. harrynesbitt

    harrynesbitt

    Joined:
    Feb 11, 2013
    Posts:
    14
    I've managed to track down the interpolation issue to the fact that I'm trying to manually rotate the player's transform (with attached rigidbody2D) while jumping, to face the direction of travel.

    I'd previously been using the rigidbody function MoveRotation(), but the new Rigidbody2D component doesn't appear to have the same function or any position/rotation variables (Script Reference). I assumed that since these options were absent, simply setting the transform would work. I was wrong!

    Can anyone tell me if there's a way to correctly set the rotation of non-kinematic rigidbody2D?