💡 Suggested Solution
You can handle player-enemy damage using FluxScript's on collision event, which fires whenever two objects with collision components overlap or contact. The typical pattern is: assign the player and enemy objects distinct tags (e.g. "Player" and "Enemy"), then in the player's script check the collided object's tag and reduce a health variable. Node Scripting has an equivalent "On Collision" event node feeding into a branch and a "Set Variable" node.
Explanation
Collision detection in Appley uses the physics system, so both objects need a Collider component (Box/Sphere/Capsule) and at least one should have a Rigidbody for on collision to fire. Health is usually stored as a numeric property on the player object so it can be referenced by UI and other systems.
Step-by-Step
-
- Select the Player object and add a Collider + Rigidbody if not present.
-
- Tag the player as "Player" and tag enemy objects as "Enemy".
-
- Add a numeric variable/property
health to the player (default 100).
-
- Attach a FluxScript (or Node graph) to the player handling the
on collision event.
-
- Inside the event, check the other object's tag and subtract 10 from
health if it is "Enemy".
-
- Optional: add invulnerability frames (cooldown timer) so the player isn't damaged every frame.
FluxScript Example
// PlayerDamage.flux
property health = 100
property invulnTime = 0
on start
health = 100
end
on update
if invulnTime > 0 then
invulnTime = invulnTime - time.delta
end
end
on collision(other)
if other.tag == "Enemy" and invulnTime <= 0 then
health = health - 10
invulnTime = 1.0 // 1 second of invulnerability
if health <= 0 then
health = 0
// trigger respawn / game over
call onPlayerDeath()
end
end
end
function onPlayerDeath()
print("Player defeated!")
// respawn logic here
end
Node Scripting Equivalent
Node Scripting graph (attach to Player):
- [On Collision] event node (output: Other Object)
- [Get Tag] node (input: Other Object) -> string
- [Branch / If] node: condition (Tag == "Enemy")
- True branch:
- [Get Property] health from Self
- [Subtract] node: health - 10
- [Set Property] health on Self
- [Set Property] invulnTime = 1.0 (optional cooldown)
- [Branch] health <= 0 -> call OnPlayerDeath function node
- False branch: (empty)
Use a separate [On Update] -> [If invulnTime > 0] -> [Subtract time.delta] -> [Set invulnTime] chain for cooldown handling.
Relevant Documentation
Performance Recommendations
- Always filter collisions by tag early (first line of the event) to avoid expensive logic running for unrelated collisions.
- Consider using collision layers/masks in the physics settings to limit which objects can collide - this is cheaper than checking tags in script.
- Avoid heavy operations (spawning particles, loading assets) directly inside
on collision; defer them with a function call or queue.
- Reuse a single invulnerability timer rather than spawning timers per hit.
- If you have many enemies, prefer pooling enemy objects rather than instantiating/destroying them on each collision.
Alternative Approaches
- For trigger-based pickups/hazards, use a Trigger Collider instead of a solid collider so movement isn't blocked.
- For knockback, apply an impulse to the player's Rigidbody inside the collision handler using physics.applyImpulse.
Best Practices
- Keep
health as a property rather than a local variable so UI elements can bind to it.
- Centralize death/respawn logic in a function so AI Director, quests, and UI can call it consistently.
- Test with multiple enemies touching the player simultaneously to ensure cooldown logic works.
- Use the Tag system consistently; define tags in Project Settings -> Tags.
ℹ️ This response contains suggested ideas. Verify against your specific use case.