Collision Detection and Events
The Character
class includes numerous events that allow you to manage different character interactions. For instance, there is OnCollided
, which is triggered when the character makes contact with a collider while executing a Move
, OnFoundGround
, which is triggered when a character detects any ground surface (whether walkable or not-walkable), among others.
When extending a Character
class, it is advisable to override its corresponding event trigger method. For example, override the OnCollided
method to handle a collision:
public class PlayerCharacter : Character
{
protected override void OnCollided(ref CollisionResult collisionResult)
{
// Call base method implementation
base.OnCollided(ref collisionResult);
// Add your code here...
Debug.Log($"Collided with {collisionResult.collider.name}");
}
}
On the other hand, when extending a Character
through composition, it's advisable to listen to its exposed events. For example, subscribe to its Collided
event to handle a collision:
public class PlayerController : MonoBehaviour
{
// The controlled Character
private Character _character;
protected void OnCollided(ref CollisionResult collisionResult)
{
Debug.Log($"Collided with {collisionResult.collider.name}");
}
private void OnEnable()
{
// Subscribe to Character events
_character.Collided += OnCollided;
}
private void OnDisable()
{
// Un-subscribe from Character events
_character.Collided -= OnCollided;
}
}
The procedure is the same for its other events.
Last updated