Collider2d



  1. Unity Circle Collider 2d
  2. Collider2d Get Contacts
  3. Collider2d Unity
  4. Collider2d Unity
  5. Unity Edge Collider 2d

Hierarchy

获取当前 Collider2D 所有重叠的 Collider2D,可添加 LayerMask、Z 轴深度等筛选条件: OverlapPoint: 判断当前 Collider2D 是否与指定二维点重叠: Raycast: 由当前Collider2D 位置发射出一条指定方向和距离的射线,返回射线碰撞结果(不包含自身),可添加 LayerMask、Z 轴深度等筛选. Script error: OnTriggerEnter2D This message parameter has to be of type: Collider2D I have a BoxCollider on an empty object that is a child of my player. When it collides nothing happens. Physics: Resizing a Collider2D now results in collision/trigger callbacks if other Collider2Ds enter/exit the collider being resized. (773735, 822612) Playables: Fixed a crash where UnloadUnusedAssets would unload Animation Clips used by AnimationClipPlayables. Unity’s Collider2D, Detecting Collisions Now that Game Objects has physical body representation, the physics engine needs a way to detects collisions between these bodies. This can be made using 2D Colliders.

  • any
    • Collider2D

Index

Properties

Accessors

Methods

Properties

Readonly TYPE

  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:126

Protected _body

_body:RigidBody2D | null = null
  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:190

Protected _density

  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:195

Protected _friction

_friction:number = 0.2
  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:199

Protected _group

  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:193

Protected _offset

Collider2d.overlapcollider
_offset:Vec2 = new Vec2()
  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:203

Protected _restitution

  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:201

Protected _sensor

_sensor:boolean = false
  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:197

Protected _shape

  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:189

editing

editing:boolean = false
  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:22

tag

  • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:28

Tag. If a node has several collider components, you can judge which type of collider is collided according to the tag.

Accessors

body

  • get body(): null | RigidBody2D
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:118

    Physics collider will find the rigidbody component on the node and set to this property.

    Returns null | RigidBody2D

density

  • get density(): number
  • set density(v: number): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:52

    Returns number

    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:55

    Parameters

    • v: number

    Returns void

friction

  • get friction(): number
  • set friction(v: number): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:80

    The friction coefficient, usually in the range [0,1].

    Returns number

    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:83

    The friction coefficient, usually in the range [0,1].

    Parameters

    • v: number

    Returns void

Unity Circle Collider 2d

group

  • get group(): number
  • set group(v: number): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:37

    Returns number

    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:40

    Parameters

    • v: number

    Returns void

impl

  • get impl(): null | IBaseShape
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:122

    Returns null | IBaseShape

offset

  • get offset(): Vec2
  • set offset(v: Vec2): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:105

    Returns Vec2

    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:108

    Parameters

    • v: Vec2

    Returns void

restitution

Collider2d Get Contacts

  • get restitution(): number
  • set restitution(v: number): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:94

    The restitution (elasticity) usually in the range [0,1].

    Returns number

    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:97

    The restitution (elasticity) usually in the range [0,1].

    Parameters

    • v: number

    Returns void

sensor

  • get sensor(): boolean
  • set sensor(v: boolean): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:66

    A sensor collider collects contact information but never generates a collision response

    Returns boolean

    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:69

    A sensor collider collects contact information but never generates a collision response

    Parameters

    • v: boolean

    Returns void

worldAABB

  • get worldAABB(): Readonly<Rect>
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:179

    Returns Readonly<Rect>

Methods

apply

Collider2d Unity

  • apply(): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:167

    If the physics engine is box2d, need to call this function to apply current changes to collider, this will regenerate inner box2d fixtures.

    Returns void

Protected onDestroy

  • onDestroy(): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:155

    Returns void

Collider2d Unity

Protected onDisable

  • onDisable(): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:149

    Returns void

Protected onEnable

  • onEnable(): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:143

    Returns void

Protected onLoad

  • onLoad(): void
    • Defined in cocos/physics-2d/framework/components/colliders/collider-2d.ts:130

    Returns void

I’ve been tinkering with Unity’s 2D tooling since it was officially released in v4.3. With each new version we see some improvements in the 2D tools and workflow, but alas it’s far from perfect and as a result getting things to work the way you want is all about trial and error.

One thing I have learnt quickly about Unity is that there are many ways to approach a problem, each with their own pros and cons. Sometimes you need to get creative with your solutions. Player colliders in Unity are one such area that that can be a struggle. Some of the problems commonly experienced when making tile based 2D games is player objects getting stuck on floors, walls and ceilings.

Below I will outline some of the problems, why they occur, and then share with you my approach, which although isn’t bulletproof, works for me.
Ray Casting is another approach which can work well, but has its own challenges to overcome. It will not however be the focus of this post.

Note* I am using Mario sprites simply for illustrative purposes. The real Mario was created long before Unity, and obviously used a different (better) system.

Getting stuck on the floor

Using a box collider with a simple Mario sprite seems like the perfect way to handle collision, but unfortunately Unity struggles with this setup and its quite common for your players collider to get caught on floor tiles that also have their own box colliders.

This has nothing to do with pixel spacing between tiles and will happen even if you have your tiles snapped together perfectly. I believe the problem comes from floating point values which are the source of many problem in various parts parts of the Unity workflow.

In some cases it may be possible to create one massive box collider for the whole floor or platform, this is a pretty limiting approach though, especially in a tile based world which can have many platform sizes.
Consider also that in a game like Mario, blocks are destructible, so the collider must be removed with the block. This would not be possible with one large collider spanning the size of a platform.

Circle Colliders

Using two circle colliders is a popular setup which I have seen used quite often in other tutorials and example projects. And though it eliminates the problem with floor stickiness, it creates new problems due to limited accuracy when jumping onto platforms (the player will roll off edges). Once again, it depends on your games requirements. If your game is a downhill runner, then sure circle colliders might do the trick, but to use a Mario platformer as an example requirement, you would need something more robust.

Hybrid Collider Setup

Which leads us to the setup which I have found works for me. It is a combination of box and circle colliders positioned in such a way to avoid obstacles getting ‘caught’.

You will need to test the sizing s against walls and floors, and make little adjustments till it feels right. Remember folks, if it feels right it is right. 😉

Remember, this approach isn’t fool proof, but it got me as close as I needed without using ray-casting.
One final step which is quite important. Add a low friction aka ‘slippery’ Physical Material to your box collider. This will stop it getting caught on walls when you are pressed up against them and attempt to jump.

Creating a PhysicalMaterial for a 2D game is easy, just right click in your project panel, and select create > Physical 2D Material

Collider2d

Unity Edge Collider 2d

Give the Physical2D material a name like ‘Slippery’ then double click it and lower the friction settings to 0.

Finally to apply it to your player’s box collider, click on your player and drag the Slippery material to the ‘Material’ slot under the box collider section in the players property panel.

Join me on Patreon to get exclusive access to source code and valuable gamedev info.

If you have any questions or suggestions drop them in the comments below.

Adios.