I’m just looking for some advice as I can’t yet decide how I want to set this up ![]()
Basically, I’m writing a power-up system for the player. I use triggers (or colliders in some places) to enable/disable power-ups as the player moves through the level.
My object is a bike moving only on X and Y - sidescrolling racing game. The bike as multiple power-ups like boost, high jump, boost ramps, ect…
Right now I have a “PowerUps.cs” script for the power-ups which controls boost, boost ramps, special boost etc… Each power-up has its own function within the script, and I setup event listeners in Awake().
To trigger the power-ups, I have a trigger/collider gameObject with a “BoostRamp.cs” | “SpecialBoost.cs” etc…script attached to it. When the player enters the trigger/collider object, I send an event to the “PowerUps.cs” script, with the correct identifier:
void OnTriggerEnter ( Collider collider )
{
Messenger<float>.Broadcast( "Power Boost", 300f );
}
But I was thinking, it might be cleaner/easier to keep each power-up code inside the power-up script which detects collision (the one attached to a collider object), and doing it this way instead:
public Transform bike;
void OnTriggerEnter ( Collider collider )
{
// Apply power boost (boost ramps, rockets, etc...), but only if moving forward
if( bike.rigidbody.velocity.x > 0 )
{
bike.rigidbody.AddRelativeForce( bike.right * boostPower, ForceMode.Impulse );
Debug.LogWarning("POWER BOOST", this);
}
}
Now my power-up specific code is only in my power-up specific script, instead of being in another script, and I don’t have to use events at all.
Is there a best practice when it comes to stuff like this? Which way is better and why? I realize it also depends on the game, and on user preference, but I also know that I have only been programming for 1 year(ish) and I have a lot to learn when it comes to writing smart code and having a nice structure which makes sense - when you come back to it a year from now ![]()
Stephane