Menu
CoddyTech

Kinematic Bodies

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 6 of 71.

Moving platforms, lifts and doors need to collide like solid objects, but they must not fall or be shoved around by whatever lands on them. That is a kinematic body: set rb.isKinematic = true, and gravity, forces and hits no longer move it. Its collider still counts, so a dynamic body (one physics moves) bumps into it as into a wall, and stands on it as on the floor. You move a kinematic body yourself, with rb.MovePosition.

This lift rises at lift m/s until it reaches top. MovePosition takes the position to be at after this step, so the code adds one step's worth of movement, using Time.fixedDeltaTime:

void Start()
{
    rb = GetComponent<Rigidbody>();
    rb.isKinematic = true;
}

void FixedUpdate()
{
    if (rb.position.y < top)
    {
        rb.MovePosition(rb.position + Vector3.up * lift * Time.fixedDeltaTime);
    }
}

Why not set transform.position? That teleports the object, and physics never learns that it moved: a crate on a sliding platform would be left behind instead of riding along. MovePosition tells physics the body travelled to the new spot during the step, so what it touches is carried and pushed properly. AddForce does nothing on a kinematic body, and kinematic bodies never collide with static colliders or with each other: only dynamic bodies run into them.

challenge icon

Challenge

Easy

Hang the barge over the lava and slide it with the arrows.

  1. In Start(), set rb.isKinematic = true;.
  2. In FixedUpdate(), read float h = Input.GetAxisRaw("Horizontal"); and call rb.MovePosition(rb.position + Vector3.right * h * speed * Time.fixedDeltaTime);.

Press Play and slide it: the barge hangs in the air, and the drone bounces off. Then Check.

Try it yourself

using UnityEngine;

public class Barge : MonoBehaviour
{
    public float speed = 2f;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        // Make the barge kinematic
    }

    void FixedUpdate()
    {
        // Slide it along x with the arrows
    }
}

All lessons in Physics and Gameplay

Practice on your own: Unity playground