“This is the 21st day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

πŸ‘‰ About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

πŸ‘‰ premise

[About the production of the ball preform]

πŸ‘‰ Practice

😜 pellet preform

Remember to create a ball each time and have a preview in the upper right corner, so we need a prefab that we can then instantiate and that will automatically randomize different balls when initialized.

Right click to create a 2D Sprite, add [CirleCollider2D] and [RigidBody2D] components, rigid body components default, collision components need to modify the Friction coefficient and elastic coefficient [Restitution], a [0.2] a [0.3] bar. 【Radius】 The Radius needs to pay attention to oh, different melon Radius is different, plus when the collision back to fit very well, this Radius needs to be very much in line with the picture, the best, can not be too large nor too small.

By the way, let’s increase the elastic coefficient, so that the rebound effect is more Q.

Then add a script, and let’s think about what the script should do

  • 1. Automatically change different spheres
  • 2. Change the radius of the current Collider for good collision detection
  • 3. Watermelon collision events and post-collision mergers
  • 4. Detection and determination of death line
// Get the Sprite
let thisSprite = this.node.getComponent(Sprite);
if(thisSprite ! =null) {
    thisSprite.spriteFrame = this.spriteFrames[index];
} else {
    console.log("ChangeBall's thisSprite is empty")}// Dynamically change the radius of the created preform
let PrefabCollider = this.node.getComponent(CircleCollider2D);
if(PrefabCollider ! =null) {
    if (index == 0) {
        PrefabCollider.radius = (54 / 2 -1);
    }
             / /... Omitting logic
    ScriptStatic.CurrentBallRadius=PrefabCollider.radius;
} else {
    console.log("ChangeBall's PrefabCollider is empty")}Copy the code

Collision detection

We are using 2D listeners registered directly against this Collider

TempCollider.on(Contact2DType.BEGIN_CONTACT, this.MethodColliderEnter, this);

The [selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null 】, official documents have the introduction, in this small empty since hina. For logical details, please refer to the source code, annotated in detail.

Note:

For a collision event to take effect, enable ContactListener must be enabled in the rigidbody component of the object.

To destroy or update a component in a collision event, use setTimeout to implement the next frame change, since the current frame change will be reported as an exception.

In the collision detection logic function, we use the TAG, and use the TAG to do some operations, so we need to pay special attention to, in the scene, the default [TAG] of some nodes is 0, it is easy to misoperate the object. So we changed the background left wall and background right Wall and background ground collision component TAG to 100 in the scene.

Then our main script uses it.

πŸ‘‰ other

πŸ“’ author: Kom and Kom in Kom

πŸ“’ reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

πŸ“’ welcome to like πŸ‘ collect 🌟 message πŸ“