Melee damage (Blender 2.46+)

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. :frowning:
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)

Nice, great tutorial. Hopefully, it will answer a lot of the questions for those creating RPGs.

5*****stars

Thanks, Sanguine.
I might research and make a ray function by myself…
Basically, to use rayCastTo(), we connect an always sensor (pulse=TRUE) instead of a ray sensor. We need to damage the enemy and then set a bool property to 1 when the ray detects something, and when it’s not detecting anything set it to 0 to reset.

rayCastTo function:
rayCastTo(object, distance, property)
So, to make this, you would use distance in a property reach, and property would be “health” (quoted because it’s a string).

hmm get error

File “Dmg_melee”, line 9, in <module>
g=GameLogic
NameError: name ‘GameLogic’ is not defined
Traceback (most recent call last):
File “Dmg_melee”, line 9, in <module>
g=GameLogic
NameError: name ‘GameLogic’ is not defined
Traceback (most recent call last):
File “Dmg_melee”, line 9, in <module>
g=GameLogic
NameError: name ‘GameLogic’ is not defined

Any1 can help me out ?

rayCastTo can also accept a position. For example:

gl = GameLogic

cont = gl.getCurrentController()
own = cont.getOwner()

#--Set the range of the weapon
range = 5

position = own.getPosition()
position[1] += range    #Add the range to the y of the owner's position

obj = own.rayCastTo(position, 0, "prop")

if obj:
    #You hit something...
    print obj.name

That should work, but I didn’t test it.

Hi, I changed the blend a little to replace the mesh with
a deformed suzzane model on the 6th strike, and I’d like to
add another layer of deformation on the 7th strike, but I can’t get it working.
I’ve tried to link the logic bricks to the deformed model also,
but the bricks aren’t carried over onto the replaced mesh.
I barely know what I’m doing though :smiley:
Maybe can someone look at this to see what’s wrong?

Attachments

damageScript replace suzzane.blend (252 KB)

Because it’s only using ONE prop.
Delete the “health2” property and on the last sensor change the prop to health and the values of “sensor” to 0.001 to 6 and of the “dead” one from 3 to 6.
Now it should work. I did test it.

Thanks, that works!
I added the health2 prop because I don’t know what I’m doing. :smiley:
I also tried to add an extra health2 line into the python with no success :smiley:

cool script i have a question though it might seem stupid but i’ll explain my thinking, would it be better to use python scripts instead of logic bricks performance wise so what i mean is like take your character movement and eve though you can do it with logic bricks you write a script instead wich would be better on things like framerate.

Longest sentence I’ve ever seen…
Anyway, using python script or logic bricks doesnt make really change the performance. If you are the kind of person who wants thing organized and in order, using scripts might be what you’ll aim for the most. When you make a rather big game with a lot of logic bricks it’ll turn out to be a mess pretty quick.

It depends on how you make it, I once made a FPS setup with logic bricks, it was ultra-slow and ultra-messy, making it in Python made it 100x faster.
That the logicbricks are written in C++ doesn’t mean they’re as efficient or as fast. I believe Python’s written on C, which may be a little faster than C++ :stuck_out_tongue:
LogicBricks can be more efficient than Python sometimes, but just sometimes, when you can do with like 4 logic bricks without needing a Python script.

Yeah. If you can achieve in thirty lines of code what you can do in 200 logic bricks, it is probably going to be more efficient.

Igsgold offers a series of excellent services for you to enjoy colourful Warhammer life. You can buy unquestionable Cheap Warhammer power leveling from us, a professional, loyal and reliable Warhammer Online powerleveling Gold exchange corporation and power leveling work group. Our professional fellows work hard and strive to make the price of Warhammer Gold lowest, please check War power leveling on every server. Nowhere else can you find such a dirt sale! Available stock of Warhammer Gold all the time, so that we can do a really instant way of delivery. Join us and get your Cheap Warhammer Gold now! You will enjoy our first class service and save more.An American friend has invited you to World of Warcraft cd key visit his family. You’ve never been to an American’s home before, and you’re not sure what to do. Should you take a gift? How should you dress? What time should you buy wow cd key arrive? What should you do when you get there? Glad you asked.

Guys is there actually a way to make health decrease randomly… for exmaple… health = 100 (so when u hit the enemy health decreases randomly and not always for example -7 -7 -7 -7 -7