Index of every page on this site: llms.txt
All skills
Run it yourself

Unity 6.3 LTS physics in 3D and 2D: Rigidbody and Rigidbody2D, forces and linearVelocity, the layer collision matrix and raycasts. Use when an object falls through the floor, OnTriggerEnter never fires, or a ground ray hits the player. Not for engine-neutral tunnelling tuning (`physics-tuning`), tilemap colliders (`unity-tilemap-2d`) or NavMesh (`unity-navmesh`).

Use this Skill: https://skilld.dev/gh/gamedev-skills/awesome-gamedev-agent-skills/unity-physics

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesphysics-2d.md

≈1.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

2D physics patterns (Unity 6.3 LTS, Rigidbody2D)

Depth for unity-physics. All types are in UnityEngine. Checked against the 6000.3 Scripting API (Rigidbody2D, Physics2D, ContactFilter2D, PlatformEffector2D) and the C# reference source at tag 6000.3.28f1.

Contents

  • Pick the body type
    1. Dynamic platformer mover
    1. Ground check with a contact filter
    1. Kinematic character with Slide
    1. One-way platform
    1. Layers and simulation settings in code
  • Read-back checks

Pick the body type

bodyType Moved by Use for
Dynamic forces, gravity, collisions players and props that react physically
Kinematic your code (MovePosition, linearVelocity, Slide) moving platforms, scripted characters
Static never level geometry (a Collider2D with no Rigidbody2D is also static)

A collider that moves every frame should sit on a Kinematic body rather than be static: moving a static collider forces the engine to rebuild it.

1. Dynamic platformer mover

using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class Runner2D : MonoBehaviour
{
    [SerializeField] private float speed = 6f, jumpSpeed = 12f;
    private Rigidbody2D _rb;
    private float _moveX;        // set from input in Update
    private bool _jumpQueued;    // set from input in Update, consumed once

    private void Awake()
    {
        _rb = GetComponent<Rigidbody2D>();
        _rb.interpolation = RigidbodyInterpolation2D.Interpolate;  // smooth under a following camera
        _rb.freezeRotation = true;                                 // no tipping over
    }

    private void FixedUpdate()
    {
        _rb.linearVelocityX = _moveX * speed;   // keep Y so gravity still applies
        if (_jumpQueued)
        {
            _rb.linearVelocityY = jumpSpeed;
            _jumpQueued = false;
        }
    }
}

2. Ground check with a contact filter

IsTouching reads contacts the solver already found, so it has no ray to start inside the player's own collider. The normal-angle range keeps walls and ceilings out.

[SerializeField] private LayerMask groundMask;
private ContactFilter2D _groundFilter;

private void Start()
{
    _groundFilter = new ContactFilter2D { useTriggers = false };
    _groundFilter.SetLayerMask(groundMask);
    _groundFilter.SetNormalAngle(45f, 135f);   // surface normals pointing roughly up
}

private bool IsGrounded() => _rb.IsTouching(_groundFilter);

A cast that reuses one buffer (the 2D *NonAlloc methods are deprecated in Unity 6):

private readonly RaycastHit2D[] _hits = new RaycastHit2D[4];

private bool GroundBelow(float distance) =>
    Physics2D.Raycast(transform.position, Vector2.down, _groundFilter, _hits, distance) > 0;

3. Kinematic character with Slide

Rigidbody2D.Slide moves the body along surfaces, applies the SlideMovement.gravity it is given and stops on slopes steeper than surfaceSlideAngle. Unity's own example calls it from Update with Time.deltaTime; it returns the hits and the remaining velocity.

[SerializeField] private Rigidbody2D.SlideMovement slide = new Rigidbody2D.SlideMovement();
private Rigidbody2D _rb;

private void Awake()
{
    _rb = GetComponent<Rigidbody2D>();
    _rb.bodyType = RigidbodyType2D.Kinematic;
}

// Call from Update; returns true when the body is standing on a surface.
public bool Move(float moveX, float speed)
{
    Rigidbody2D.SlideResults r = _rb.Slide(new Vector2(moveX * speed, 0f), Time.deltaTime, slide);
    return r.surfaceHit;   // RaycastHit2D converts to bool
}

4. One-way platform

Add a PlatformEffector2D to the platform, set useOneWay = true and surfaceArc (180 is the top half), and tick Used By Effector on its collider (collider.usedByEffector = true in code). Without that tick the effector does nothing. To drop through, briefly ignore the pair: Physics2D.IgnoreCollision(playerCollider, platformCollider, true), then set it back.

5. Layers and simulation settings in code

int player = LayerMask.NameToLayer("Player"), enemy = LayerMask.NameToLayer("Enemy");
Physics2D.IgnoreLayerCollision(player, enemy, true);           // separate from the 3D matrix
bool ignored = Physics2D.GetIgnoreLayerCollision(player, enemy);

Physics2D.simulationMode is FixedUpdate by default. Update steps once per frame; Script never steps until you call Physics2D.Simulate(deltaTime). A project that "has no gravity" often has m_SimulationMode changed in ProjectSettings/Physics2DSettings.asset.

Read-back checks

With the Unity CLI and a ready Editor, in Play mode:

unity command script eval --code 'var rb = Object.FindAnyObjectByType<Rigidbody2D>(); return rb.name + " " + rb.bodyType + " v=" + rb.linearVelocity + " sim=" + rb.simulated;'

Without it, put the same values in a PlayMode test assertion or a one-off Debug.Log and read the Editor log.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts8d3 checks · Risk SAFE
  • Gen Agent Trust Hub9d

    This skill provides best practices, C# code snippets, and verification workflows for Unity 6.3 LTS physics. It references official Unity resources and uses standard CLI commands for project interaction.

  • Socket8d

    No alerts

  • Snyk9d

    Risk: LOW · No issues

Signed by skilld at c6ca6c4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last week.

Activeupdated last week

README badge

README badge for gamedev-skills/awesome-gamedev-agent-skills/unity-physics