In this short tutorial I’ll show you how to make melee damage for RPGs or other melee weapon fighting games, it’s not that good for hand to hand but it works. It works better with weapons such as swords, spears, axes, etc.
This tutorial is intended for beginners and people who don’t know much Python.
You should already know how to use logic bricks, add new windows, move objects on the viewport, and add and connect scripts to logic bricks, change between modes (object and pose modes), know what is an axis, know how to handle properties, know how to make enemies and their health, know how to make characters, know how to rig and animate, know how to change the value of properties via actuators, know how to copy and paste text, know how to move the mouse and click on things, know how to read english, know how to type in the keyboard.
WARNING: If you don’t know any of these things listed above, DON’T ask me how to do them. Instead use this super-cool tool, because I am not patient.
I didn’t ask everyone about the basics when I was beginning… I didn’t come with “howd i do thiz and tha and theh, i wana make a mmoorpg”.
When I asked something 99.9% of the time was because something I made wasn’t working, like a Python script.
If something that should be working for you isn’t working, feel free to ask what’s going on!
This only works with versions of blender with parent to bone.
You should already have the character ready with the weapon in hand, all animated.
1. Add an empty. (Space->Add->Empty).
2. Position it to the character’s hand.
3. With the empty selected, position it at the hand and make the Y-Axis of the empty point to the point of your weapon.
4. Add a Ray sensor, pointing to the +Y axis, and property set to health or whatever you’re using for the health of the enemy. Set the range to the distance from the empty to the point of your weapon (no, it doesn’t have to be exact)
5. Add 1 float property, called exactly like this:
dmg (set the value to the damage you want your weapon to do)
Unfortunately, there is no way to change the range of the ray via Python. 
There is no setRange() function.
You could, however, move the empty using IPOs, or use the rayCastTo() function which needs a second object…
6. Add a script controller, in the Text window, add a new script and name it “MeleeDamage.py”.
Set the script controller to “MeleeDamage.py”
Now, copy and paste this on the script:
#Melee Damage Script by B3D00
#Use: Set damage property to what
#damage you want your weapon to do, and the ray
#distance to how long your weapon is.
#parent the empty to a bone (when entering pose mode)
#and place at bone's position like in example file.
g=GameLogic
c=g.getCurrentController()
o=c.getOwner()
ray=c.getSensor("swordsharp")
hitobj=ray.getHitObject()
hitobj.health-=o.dmg
It’s actually very basic.
In the last line, where it says hitobj.health, change it to hitobj.(whatever is the property of the enemy’s health)
If it’s already just called health, leave it like that. It should be in the enemy’s collision box or mesh.
That should do it! Whenever the weapon touches an enemy, it’s health will be decreased.
If you want, I’ll make a script that uses rayCastTo(). But you will need a second empty.
I don’t think the ray will be simply as long as from one empty to another, but the ray will just point to the second empty.
Attachments
damageScript.blend (137 KB)

