Addition, Subtraction, Multiplication, Division Functions
Arithmetic operations are needed in games for score calculation, health restoration, damage calculation, etc.
public class GameCalculator : MonoBehaviour
{
// Health restoration (addition)
int HealPlayer(int currentHp, int healAmount)
{
return currentHp + healAmount; // Add
}
// Take damage (subtraction)
int TakeDamage(int currentHp, int damage)
{
return currentHp - damage; // Subtract
}
// Experience gain (multiplication - bonus multiplier)
int GetExperienceWithBonus(int baseExp, int bonusMultiplier)
{
return baseExp * bonusMultiplier; // Multiply
}
// Health percentage calculation (division)
float GetHealthPercentage(int currentHp, int maxHp)
{
return (float)currentHp / maxHp; // Divide (cast to float needed)
}
}
Usage Example
public class Player : MonoBehaviour
{
public int hp = 100;
public int maxHp = 100;
public int exp = 0;
void Start()
{
// Restore health
hp = HealPlayer(hp, 30); // 100 + 30 = 130
Debug.Log("Health: " + hp);
// Take damage
hp = TakeDamage(hp, 25); // 130 - 25 = 105
Debug.Log("Health: " + hp);
// Experience bonus (2x)
exp = GetExperienceWithBonus(50, 2); // 50 * 2 = 100
Debug.Log("Experience: " + exp);
// Calculate health percentage
float healthPercent = GetHealthPercentage(hp, maxHp); // 105 / 100 = 1.05
Debug.Log("Health percentage: " + (healthPercent * 100) + "%");
}
}
Actual game usage:
public class CombatSystem : MonoBehaviour
{
// Damage calculation (combining multiple factors)
int CalculateDamage(int baseDamage, int weaponPower, float criticalMultiplier)
{
int total = baseDamage + weaponPower; // Addition
return Mathf.RoundToInt(total * criticalMultiplier); // Multiplication
}
// Health restoration (with limit)
int HealWithLimit(int currentHp, int healAmount, int maxHp)
{
int newHp = currentHp + healAmount; // Addition
if (newHp > maxHp)
{
return maxHp; // Max limit
}
return newHp;
}
}
Function Naming Tips
Function names should clearly indicate what they do. In Unity game development, use names that clearly express actions.
-
✅ Good examples:
CalculateDamage- Calculate damageGetPlayerHP- Get player healthHealPlayer- Heal playerSpawnEnemy- Spawn enemyCheckCollision- Check collision
-
❌ Bad examples:
func1,test,abc- Cannot tell what they doDoSomething- Too vagueCalc- Avoid abbreviations
Naming rules:
- Start with a verb (Calculate, Get, Set, Check, Spawn, etc.)
- Use clear and specific names
- Not too long (recommended under 20 characters)
Function Writing Checklist
-
Function name: Does it clearly express the functionality?
- Example:
CalculateDamage✅ vsCalc❌
- Example:
-
Parameters: Does it receive all necessary values?
- Example:
HealPlayer(int currentHp, int healAmount)✅ - Example:
HealPlayer()- Don't know how much to heal ❌
- Example:
-
Return value: Does it return the result properly?
- Example:
int GetPlayerLevel() { return level; }✅ - Example:
void GetPlayerLevel() { Debug.Log(level); }- Doesn't return ❌
- Example:
-
Reusability: Can it be used in other places?
- Example:
CalculateDamage(int base, int weapon)- Can use with various weapons ✅ - Example:
CalculateSwordDamage()- Only usable with sword ❌
- Example:
Additional checklist for game development: 5. Error handling: Is there handling for invalid input? (division by zero, etc.) 6. Unity integration: Does it integrate well with Unity components? 7. Performance: Are there unnecessary calculations?