Easy H2H Attacks
by Quest Johnny · in Torque Game Engine · 08/13/2004 (5:52 am) · 10 replies
This is the code I added for *simple* unarmed Hand to Hand attacks between players. If you want more complex code, there are quite a few great resources for melee weapons.
It targets a point out a certain "attack distance" (which is large here, because it's for a racing game) out in front of your chr. It won't target things off to the sides, or that have passed your chr.
Your fire code calls:
function CrossbowImage::onFire(%this, %obj, %slot)
{
echo("weapon firing");
H2H::attack(%this,%obj,%slot,20);
...
function H2H::attack(%weaponImage,%obj,%slot,%damage) {
//alright.. so I'm going to try and cast a box from a point from your monster's
//current heading/position
%attackDistance = 20;
echo("chr position=" SPC %obj.getTransform() );
%vector = %obj.getMuzzleVector(%slot);
echo("nVector=" SPC %vector);
//%objectVelocity = %obj.getVelocity(); //current speed of chr
%vector = VectorNormalize(%vector); //normalize
%vector = VectorScale(%vector,%attackDistance); //at a guess?
echo("nVector=" SPC %vector);
%eye = %obj.getTransform();
echo("eye=" SPC %eye);
%xx = getWord(%eye, 0);
%yy = getWord(%eye, 1);
%zz = getWord(%eye, 2);
%vec = (%xx SPC %yy SPC %zz);
echo("vec=" SPC %vec);
//%found = ContainerFindFirst(mask,
%searchPos = VectorAdd(%vec,%vector);
echo("searchPos=" SPC %searchPos);
InitContainerRadiusSearch(%searchPos, %attackDistance, $TypeMasks::PlayerObjectType);
while ((%targetObject = containerSearchNext()) != 0) {
echo("found=" SPC %targetObject );
if(%targetObject != %obj ) {
//hey it's not us!
%targetObject.applyDamage(20); //20 pts damage?
}//endif
}//endwhile
}//endF
Hope this helps!
It targets a point out a certain "attack distance" (which is large here, because it's for a racing game) out in front of your chr. It won't target things off to the sides, or that have passed your chr.
Your fire code calls:
function CrossbowImage::onFire(%this, %obj, %slot)
{
echo("weapon firing");
H2H::attack(%this,%obj,%slot,20);
...
function H2H::attack(%weaponImage,%obj,%slot,%damage) {
//alright.. so I'm going to try and cast a box from a point from your monster's
//current heading/position
%attackDistance = 20;
echo("chr position=" SPC %obj.getTransform() );
%vector = %obj.getMuzzleVector(%slot);
echo("nVector=" SPC %vector);
//%objectVelocity = %obj.getVelocity(); //current speed of chr
%vector = VectorNormalize(%vector); //normalize
%vector = VectorScale(%vector,%attackDistance); //at a guess?
echo("nVector=" SPC %vector);
%eye = %obj.getTransform();
echo("eye=" SPC %eye);
%xx = getWord(%eye, 0);
%yy = getWord(%eye, 1);
%zz = getWord(%eye, 2);
%vec = (%xx SPC %yy SPC %zz);
echo("vec=" SPC %vec);
//%found = ContainerFindFirst(mask,
%searchPos = VectorAdd(%vec,%vector);
echo("searchPos=" SPC %searchPos);
InitContainerRadiusSearch(%searchPos, %attackDistance, $TypeMasks::PlayerObjectType);
while ((%targetObject = containerSearchNext()) != 0) {
echo("found=" SPC %targetObject );
if(%targetObject != %obj ) {
//hey it's not us!
%targetObject.applyDamage(20); //20 pts damage?
}//endif
}//endwhile
}//endF
Hope this helps!
About the author
#2
08/13/2004 (8:10 pm)
I want to use something similar to grab nearby Static Shapes, but all the masks I try don't grab anything.. or too much.. what is the mask for static shapes? I've tried $TypeMasks::StaticShapeObjectType) and StaticTSObjectType
#3
08/13/2004 (9:18 pm)
You can always just filter by getClassName() or similar...
#4
I still haven't figured out where SPC is any better than hitting the space bar in situations like...
echo("chr position=" SPC %obj.getTransform() );
You could have typed even faster using...
echo("chr position= " @ %obj.getTransform() );
On the other hand, this is a perfect use for it...
%vec = (%xx SPC %yy SPC %zz);
because it would take longer to type this...
%vec = (%xx @ " " @ %yy @ " " @ %zz);
BTW, not griping or criticizing, just an observation.
08/13/2004 (11:37 pm)
Man, I have never seen anyone use SPC so much, lol.I still haven't figured out where SPC is any better than hitting the space bar in situations like...
echo("chr position=" SPC %obj.getTransform() );
You could have typed even faster using...
echo("chr position= " @ %obj.getTransform() );
On the other hand, this is a perfect use for it...
%vec = (%xx SPC %yy SPC %zz);
because it would take longer to type this...
%vec = (%xx @ " " @ %yy @ " " @ %zz);
BTW, not griping or criticizing, just an observation.
#5
08/14/2004 (6:52 am)
I want to use something similar to grab nearby Static Shapes, but all the masks I try don't grab anything.. or too much.. what is the mask for static shapes? I've tried $TypeMasks::StaticShapeObjectType) and StaticTSObjectType
#6
function H2H::attack(%weaponImage,%obj,%slot,%damage) {
//alright.. so I'm going to try and cast a box from a point from your monster's
//current heading/position
%attackDistance = 20;
%destroyDistance = 10;
%eye = %obj.getTransform();
echo("eye=" @ %eye);
%xx = getWord(%eye, 0);
%yy = getWord(%eye, 1);
%zz = getWord(%eye, 2);
%vec = (%xx SPC %yy SPC %zz);
echo("vec=" @ %vec);
%muzzleVector = %obj.getMuzzleVector(%slot);
echo("muzzleVector=" @ %muzzleVector);
%muzzleVector = VectorNormalize(%muzzleVector); //normalize
//%objectVelocity = %obj.getVelocity(); //current speed of chr
%vector = VectorScale(%muzzleVector,%attackDistance); //at a guess?
echo("vector=" @ %vector);
//%found = ContainerFindFirst(mask,
%searchPos = VectorAdd(%vec,%vector);
echo("searchPos=" @ %searchPos);
//check for other players - do damage to them!
InitContainerRadiusSearch(%searchPos, %attackDistance, $TypeMasks::PlayerObjectType);
while ((%targetObject = containerSearchNext()) != 0) {
echo("found=" @ %targetObject );
if(%targetObject != %obj ) {
//hey it's not us!
%targetObject.applyDamage(20); //20 pts damage?
}//endif
}//endwhile
%vector = VectorScale(%muzzleVector,%destroyDistance); //at a guess?
echo("vector=" @ %vector);
//%found = ContainerFindFirst(mask,
%searchPos = VectorAdd(%vec,%vector);
echo("searchPos=" @ %searchPos);
//now, did we slay any destroyables(items)
InitContainerRadiusSearch(%searchPos, %destroyDistance, $TypeMasks::StaticObjectType);
while ((%targetObject = containerSearchNext()) != 0) {
echo("also found=" @ %targetObject SPC %targetObject.getClassName() );
if(%targetObject.getClassName() $= "TSStatic")
echo("destroy=" @ %targetObject );
//it gets destroyed
}//endwhile
}//endF
08/14/2004 (6:53 am)
Not as efficient as I'd like, but it does work. Updated code IS:function H2H::attack(%weaponImage,%obj,%slot,%damage) {
//alright.. so I'm going to try and cast a box from a point from your monster's
//current heading/position
%attackDistance = 20;
%destroyDistance = 10;
%eye = %obj.getTransform();
echo("eye=" @ %eye);
%xx = getWord(%eye, 0);
%yy = getWord(%eye, 1);
%zz = getWord(%eye, 2);
%vec = (%xx SPC %yy SPC %zz);
echo("vec=" @ %vec);
%muzzleVector = %obj.getMuzzleVector(%slot);
echo("muzzleVector=" @ %muzzleVector);
%muzzleVector = VectorNormalize(%muzzleVector); //normalize
//%objectVelocity = %obj.getVelocity(); //current speed of chr
%vector = VectorScale(%muzzleVector,%attackDistance); //at a guess?
echo("vector=" @ %vector);
//%found = ContainerFindFirst(mask,
%searchPos = VectorAdd(%vec,%vector);
echo("searchPos=" @ %searchPos);
//check for other players - do damage to them!
InitContainerRadiusSearch(%searchPos, %attackDistance, $TypeMasks::PlayerObjectType);
while ((%targetObject = containerSearchNext()) != 0) {
echo("found=" @ %targetObject );
if(%targetObject != %obj ) {
//hey it's not us!
%targetObject.applyDamage(20); //20 pts damage?
}//endif
}//endwhile
%vector = VectorScale(%muzzleVector,%destroyDistance); //at a guess?
echo("vector=" @ %vector);
//%found = ContainerFindFirst(mask,
%searchPos = VectorAdd(%vec,%vector);
echo("searchPos=" @ %searchPos);
//now, did we slay any destroyables(items)
InitContainerRadiusSearch(%searchPos, %destroyDistance, $TypeMasks::StaticObjectType);
while ((%targetObject = containerSearchNext()) != 0) {
echo("also found=" @ %targetObject SPC %targetObject.getClassName() );
if(%targetObject.getClassName() $= "TSStatic")
echo("destroy=" @ %targetObject );
//it gets destroyed
}//endwhile
}//endF
#7
08/14/2004 (7:06 am)
This is very intereting; can this system be used for melee type combat without any type of weapon[guess that's what H2H is], except the natural claws/fangs/etc...abilities of an animal or creature? As an example; I have some critters[snake,spider,bear,etc...], and I would like them to attack as a member of AI class, is this the approach? or do I need to create a 'dummy' weapon DTS and attach it to the shape as the current player and ai function. I see the above example works off of the CrossbowWeapon firing code, does that mean when I spawn my critter, it needs a .setInventory(crossbowImage,0)to be ready to attack? I guess I'm getting this and AI confused. Thanks.
#8
The code above doesn't use projectiles, but it DOES use the muzzleVector from the weapon in slot 0.
H2H = Hand "to" Hand
08/14/2004 (7:51 am)
This is designed specifically to be for simple h2h attacks by critters (in this case werewolves, vampires, etc) You'd have to hook in the detection-hit code into your bot AI as well as / instead of your onFire.The code above doesn't use projectiles, but it DOES use the muzzleVector from the weapon in slot 0.
H2H = Hand "to" Hand
#9
one thing I'm doing that you might be interested in is:
in my animation I have a trigger set. When that trigger goes off I do my melee hit detection.
so when I start a melee attack, I just play the correct animation. then when the callback for hit detection goes off I do the check. Good for timing things to animations. You could also set that up to have multiple hit detections for a single animation, picturing werewolf lashing out with the right then the left claws :)
12/29/2004 (5:52 pm)
Andrew good job man, I like this kind of stuff beter than the other super detailed melee systems.one thing I'm doing that you might be interested in is:
in my animation I have a trigger set. When that trigger goes off I do my melee hit detection.
so when I start a melee attack, I just play the correct animation. then when the callback for hit detection goes off I do the check. Good for timing things to animations. You could also set that up to have multiple hit detections for a single animation, picturing werewolf lashing out with the right then the left claws :)
#10
01/15/2011 (5:14 pm)
I have a question, how do you make your model use this script to perform a melee attack? All help appreciated.
Associate Kyle Carter