- Home /
Moving kinematic rigidbody causing strange physics behaviour, is this a bug?
Since updating to Unity 2017 I have noticed a strange physics behaviour. My game uses moving platforms that are kinematic rigidbodys that move with Rigidbody.MovePosition(). This method is used so that the moving platforms work perfectly in the physics system, updating their position will not work perfectly with continuous collisions.
Moving a physics based player character to where one such moving platform has been often causes its physics to glitch out.
I made a simple mockup example that has the same problem:
I created 2 cubes, one made large and flat to serve as ground, the other gets a rigidbody that is kinematic and has continuous collision and the following script attached:
using UnityEngine;
public class KinematicOscilate : MonoBehaviour {
public float timeScale = 0.04f;
public Vector3 distance = new Vector3(0f,6f,0f);
float time = 0f;
Rigidbody rig;
private void Start()
{
rig = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
time += Time.deltaTime * timeScale;
if (time > 1)
{
time--;
}
rig.MovePosition( distance * Mathf.Sin(time * Mathf.PI * 2) );
}
}
Then I created a sphere. Added a rigidbody with continuous dynamic collision and added this script:
using UnityEngine;
public class BallMovement : MonoBehaviour {
public float multiplier = 1000f;
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
}
void FixedUpdate () {
rig.AddForce(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"))* multiplier * Time.deltaTime * rig.mass);
}
}
This ball and the moving platform are placed on the ground in line with one another.
When running this example, after waiting for the cube to rise, and then fall through the ground, moving the ball back and forth where the cube has been causes it to glitch out and get stuck in the ground.
Thinking the issue could be related to the new deferred physics transform updates I tried inserting Physics.SyncTransorms in various places with no success.
Going back to Unity 5 has resolved my issue. However the same glitchy behaviour still occurs in both Unity 5 and Unity 2017 when using animate physics to control the platform ins$$anonymous$$d of Rigidbody.$$anonymous$$ovePosition() in FixedUpdate.