Need help with combat system

How can I get it to where if a entity collides with a specific item (such as the player’s weapon) it takes damage, and if it takes too much it is destroyed. How would I code that?

You could take a look at the Pong project. It detects when the ball hits a wall and plays a sound:

Cutting out the important code for the ball, you have:

    initialize: function () {
        this.entity.collision.on('collisionstart', this.onHit, this);

    onHit: function (result) {'beep.mp3');

To destroy and entity, you just call:


Simple. :smile:

No, not so simple. I need it to where the player’s weapon is the only thing that can damage it. This code is done to where anything it touches can damage it. It needs to have some sort of mask, or it must be assigned the specific groups of items it can collide with. Thanks for the idea, but I still need help.

The result parameter has a reference to the entity hitting it. So you can do:

onHit: function (result) {
    if ( === 'Axe') { -= 10;
        if ( <= 0) {

the part ‘Axe’, how does that apply to what object is hitting it? is the objects name, the objects collision asset? what is it?

That’s the name of the Entity as set in the Editor. It could be anything.

The name of the item that damages the target is called the “MainHand”. the MainHand entity has a script that allows it to actually function like a hand making it able to hold an object and swap the item it is holding with another. How can I add to your code a way to get the target to recognize the item the MainHand is holding? This is important because each weapon the player can hold should do a specific amount of damage. For example, the small axe does 10 damage, the medium axe does 15, and the large axe does 25. The item the MainHand is holding is really just the MainHand’s model component. Here is the MainHand’s script:

when a key is pressed (keys 1 - 5), the model component for the MainHand changes to a different model. What I need to know is how will the target take damage according to the amount each weapon is sopposed to do when it is hit by it?

you need a global variable i guess in player script would be the best, when you change the weapon the value of the damage varibale change as well, you can also use a min/max damage for every weapon and the damage can change based on target armor so you need a damage calculator like
you can add also some other variable as distance
ps the damage calculator as to be rewritten in the right way this is just an example

I have not learned how to use global variables yet …

That would mean I would have to create a script for both the weapon and the target what would these codes look like? It seems to me that there would be two scripts working together to form the two halves to a larger function. How would I code the half that sets each weapon’s damage, then the half for the enemy’s health system?

look at my newer topic “The game’s combat system”.