Collision
Collision System Overview:
The provided code defines an abstract class CollisionBase
that serves as the foundation for a collision system in a Unity game. This system is designed to handle collision events (enter, stay, exit) involving a specified type of collider (TCollider
). The implementation includes event handling, collision detection, and collider management.
Generic CollisionBase Class:
This is an abstract class (
CollisionBase<TCollider, TPhysicScene>
) that serves as the base for collision handling.It extends
MonoBehaviour
and implements theIEditableForEditor
interface.It has three events (
OnCollisionEnter
,OnCollisionStay
,OnCollisionExit
) that can be subscribed to for reacting to collisions.It includes properties and methods for managing collision layers, garbage collider size, center position, and physics scenes.
Colliders and Garbage Collection:
The class handles collisions using an array of colliders (
m_garbageColliders
) and a list (_colliders
) to keep track of colliders involved in collisions.The size of the garbage collider array is set by
GarbageColliderSize
.The
HandleColliderCleaner
method is responsible for cleaning up null colliders from the list and resetting the garbage colliders.
Physics Scene and Layer Management:
The class has a physics scene (
TPhysicScene m_physicsScene
) that is updated using theUpdatePhysicScene
method.The collision layer is set using the
SetLayer
method, which takes aLayerMask
parameter.
Collision Handling:
The main collision handling logic is in the
FixedUpdate
method, which is executed at a fixed time interval.The
CalculateCollision
method is abstract and should be implemented by subclasses to define specific collision logic.The class handles new collisions, continued collisions, and collisions exits using the
HandleNewCollisions
,HandleContinuedCollisions
, andHandleCollisionsExit
methods, respectively.
Event Handling:
The class provides methods (
HandleCollisionEnter
,HandleCollisionStay
,HandleCollisionExit
) to trigger the corresponding events when collisions occur.
Editor Integration:
The class implements an interface (
IEditableForEditor
) to support editor-specific functionality. TheEditable
property is likely used for toggling whether the collision system is editable in the Unity Editor.
Initialization:
The
Awake
method sets the initial capacity of colliders and garbage colliders using theSetCollidersCapacity
method.The
Start
method initializes the physics scene usingGetPhysicScene
.
Error Handling:
If the physics scene is not set during the
FixedUpdate
, an error message is logged to the console.
How to Use:
Inheritance:
Create a class that inherits from
CollisionBase
.Implement the abstract methods (
CalculateCollision
andGetPhysicScene
) to define specific collision behavior.
Event Handling:
Subscribe to the collision events (
OnCollisionEnter
,OnCollisionStay
,OnCollisionExit
) in the derived class to execute custom logic when collisions occur.
Unity Editor Integration:
Utilize the
Editable
property for debugging purposes, making the collision system visually editable in the Unity Editor.
Layer Filtering:
Set the
m_layer
property to filter collisions based on layers.
Example Usage:
Benefits:
Provides a flexible foundation for implementing collision systems in Unity.
Offers a modular structure for handling different types of colliders and physics scenes.
Enables event-driven collision handling for easy customization.
Considerations:
Ensure that the abstract methods are implemented in derived classes to define specific collision behavior.
Customize the event handling methods (
HandleCollisionEnter
,HandleCollisionStay
,HandleCollisionExit
) for the desired behavior in the derived class.The system is designed for 3D physics; modifications may be necessary for 2D physics.
Last updated