Raycast Trouble
by Richard_H · in Torque Game Engine · 09/12/2006 (3:45 pm) · 17 replies
Hi
I'm trying to make a tazer weapon which performs a raycast 5 meters in the direction the tazer is facing and damages + pushs the first player hit. Unfortunetly I cannot seem to get it to work.
I think it might be the TypeMask or the method used to get the endpoint of my ray. Any sugestions would be greatly apreciated.
function TazerImage::onFire(%this, %obj, %slot)
{
%muzzleVector = %obj.getMuzzleVector(%slot);
%forwardVec = %obj. getForwardVector();
%endPoint = VectorScale(%forwardVec,5);
%nearID = ContainerRayCast(%muzzleVector,%endPoint,$TypeMasks::PlayerObjectType);
if(%nearID != 0)
{
echo("We targeted someone!");
%nearID.applyImpulse(%nearID.getPosition(),VectorScale(%forwardVec,25));
%nearID.damage(30);
}
return %p;
}
I'm trying to make a tazer weapon which performs a raycast 5 meters in the direction the tazer is facing and damages + pushs the first player hit. Unfortunetly I cannot seem to get it to work.
I think it might be the TypeMask or the method used to get the endpoint of my ray. Any sugestions would be greatly apreciated.
function TazerImage::onFire(%this, %obj, %slot)
{
%muzzleVector = %obj.getMuzzleVector(%slot);
%forwardVec = %obj. getForwardVector();
%endPoint = VectorScale(%forwardVec,5);
%nearID = ContainerRayCast(%muzzleVector,%endPoint,$TypeMasks::PlayerObjectType);
if(%nearID != 0)
{
echo("We targeted someone!");
%nearID.applyImpulse(%nearID.getPosition(),VectorScale(%forwardVec,25));
%nearID.damage(30);
}
return %p;
}
About the author
#2
Returns a Vector relative to the player, so its NOT a starting position.
Also, ContainerRayCast Returns a bunch of info, NOT just the object hit. the first word in the string is the object hit.
use
%MuzzlePoint = %obj.getMuzzlePoint(%slot);
%muzzleVector = %obj.getMuzzleVector(%slot);
%endPoint = Vectoradd(%MuzzlePoint,VectorScale(%muzzleVector,5));
%ray = ContainerRayCast(%MuzzlePoint,%endPoint,$TypeMasks::PlayerObjectType);
%nearID=getWord(%ray, 0);
that should work for the ray part
09/12/2006 (4:35 pm)
%obj.getMuzzleVector(%slot);Returns a Vector relative to the player, so its NOT a starting position.
Also, ContainerRayCast Returns a bunch of info, NOT just the object hit. the first word in the string is the object hit.
use
%MuzzlePoint = %obj.getMuzzlePoint(%slot);
%muzzleVector = %obj.getMuzzleVector(%slot);
%endPoint = Vectoradd(%MuzzlePoint,VectorScale(%muzzleVector,5));
%ray = ContainerRayCast(%MuzzlePoint,%endPoint,$TypeMasks::PlayerObjectType);
%nearID=getWord(%ray, 0);
that should work for the ray part
#3
09/12/2006 (7:13 pm)
Yeah, that looks like it'd work out.
#4
09/13/2006 (4:01 am)
It seems good, all the way up to the point where it tries to damage the player. I can just stand next to the AI player while he fires it at me, and it keeps saying he hit someone and the value of %nearID is 1406, but it never pushes me or does any damage, does anyone know why? It looks like an object ID, so it should be working.
#5
09/13/2006 (11:14 am)
Try adding %obj to the end of the RayCast? Like:ContainerRayCast(%MuzzlePoint,%endPoint,$TypeMasks::PlayerObjectType, %obj);That will make sure it doesn't collide with itself, if that's what's happening. Otherwise, put echo %nearID.getName() or check the ID's in the mission editor or something to find out exactly what it's targetting.
#6
function TazerImage::onFire(%this, %obj, %slot)
{
%MuzzlePoint = %obj.getMuzzlePoint(%slot);
%muzzleVector = %obj.getMuzzleVector(%slot);
%endPoint = Vectoradd(%MuzzlePoint,VectorScale(%muzzleVector,5));
%ray = ContainerRayCast(%MuzzlePoint,%endPoint,$TypeMasks::PlayerObjectType,%obj);
%nearID = getWord(%ray, 0);
%name = eval(%nearID @ ".getName();");
echo("The ID is " @ %nearID @ " and the name is " @ getClassName());
if(%nearID != 0)
{
echo("We targeted someone!");
%nearID.applyImpulse(%nearID.getPosition(),VectorScale(%forwardVec,25));
%nearID.damage(30);
}
return %p;
}
Edit: Added code, also realized %p was still in code and am wondering how that might affect it,
09/13/2006 (3:08 pm)
It gives an error every time I try to run a function from %nearID. Plus, the TypeMask should make it only collide with players and even with %obj it still won't work, only now it returns 1416. Does anyone have any idea what is wrong with it?function TazerImage::onFire(%this, %obj, %slot)
{
%MuzzlePoint = %obj.getMuzzlePoint(%slot);
%muzzleVector = %obj.getMuzzleVector(%slot);
%endPoint = Vectoradd(%MuzzlePoint,VectorScale(%muzzleVector,5));
%ray = ContainerRayCast(%MuzzlePoint,%endPoint,$TypeMasks::PlayerObjectType,%obj);
%nearID = getWord(%ray, 0);
%name = eval(%nearID @ ".getName();");
echo("The ID is " @ %nearID @ " and the name is " @ getClassName());
if(%nearID != 0)
{
echo("We targeted someone!");
%nearID.applyImpulse(%nearID.getPosition(),VectorScale(%forwardVec,25));
%nearID.damage(30);
}
return %p;
}
Edit: Added code, also realized %p was still in code and am wondering how that might affect it,
#7
09/13/2006 (3:48 pm)
Richard post your revised code so someone can see whats wrong.
#8
I was looking at the advancedCursor resource and I noticed this. This doesn't applie since it's all done server side, right?
09/13/2006 (5:23 pm)
Maybe it has to do with client ghosting? (resolveObjectFromGhostIndex)I was looking at the advancedCursor resource and I noticed this. This doesn't applie since it's all done server side, right?
#9
Quick check: what directory and file is the script you list above in?
09/13/2006 (5:29 pm)
It depends on where you are running this code.Quick check: what directory and file is the script you list above in?
#10
theres a syntax error on this line. make sure the file is compiling without errors.
09/13/2006 (9:59 pm)
echo("The ID is " @ %nearID @ " and the name is " @ getClassName());theres a syntax error on this line. make sure the file is compiling without errors.
#11
I still have a sneaky suspicion thta he is doing this in client side script though which is why I asked my question.
09/13/2006 (11:13 pm)
Great catch Sean. What he's saying is that you can't call getClassName() as a global function--it's a namespace script method. It should be %nearID.getClassName().I still have a sneaky suspicion thta he is doing this in client side script though which is why I asked my question.
#12
Edit:I have confirmed that it is a player because of the echo, yet it still applies no damage to me.
Echo: We targeted someone!
The ID us 1416 and the name is Player
function TazerImage::onFire(%this, %obj, %slot)
{
%MuzzlePoint = %obj.getMuzzlePoint(%slot);
%muzzleVector = %obj.getMuzzleVector(%slot);
%endPoint = Vectoradd(%MuzzlePoint,VectorScale(%muzzleVector,5));
%ray = ContainerRayCast(%MuzzlePoint,%endPoint,$TypeMasks::PlayerObjectType,%obj);
%nearID = getWord(%ray, 0);
if(%nearID != 0)
{
echo("We targeted someone!");
echo("The ID is " @ %nearID @ " and the name is " @ %nearID.getClassName());
%nearID.applyImpulse(%nearID.getPosition(),VectorScale(%forwardVec,25));
%nearID.damage(30);
}
return %p;
}
09/14/2006 (3:25 am)
Well, the enemy switchs to this weapon so it must be compiling and it's all in the server/scripts directory so I'm pretty sure it's server side.Edit:I have confirmed that it is a player because of the echo, yet it still applies no damage to me.
Echo: We targeted someone!
The ID us 1416 and the name is Player
function TazerImage::onFire(%this, %obj, %slot)
{
%MuzzlePoint = %obj.getMuzzlePoint(%slot);
%muzzleVector = %obj.getMuzzleVector(%slot);
%endPoint = Vectoradd(%MuzzlePoint,VectorScale(%muzzleVector,5));
%ray = ContainerRayCast(%MuzzlePoint,%endPoint,$TypeMasks::PlayerObjectType,%obj);
%nearID = getWord(%ray, 0);
if(%nearID != 0)
{
echo("We targeted someone!");
echo("The ID is " @ %nearID @ " and the name is " @ %nearID.getClassName());
%nearID.applyImpulse(%nearID.getPosition(),VectorScale(%forwardVec,25));
%nearID.damage(30);
}
return %p;
}
#13
09/14/2006 (5:18 am)
I'm not sure if I'm missing something but this doesn't also doesn't seem right. %p is not declared?return %p;
#14
its supposed to be .applyDamage(30).
when it comes to the .applyImpulse(), for the second vector you're using %forwardVec which doesnt exist anymore due to your recent changes. use vectorsub() to obtain the vector between %MuzzlePoint and %endpoint, normalize and scale it appropriately, and use that as the second argument.
let us know if this works.
09/14/2006 (8:08 am)
OK Richard I believe the reason .damage(30) isnt working is because it doesnt exist! =)its supposed to be .applyDamage(30).
when it comes to the .applyImpulse(), for the second vector you're using %forwardVec which doesnt exist anymore due to your recent changes. use vectorsub() to obtain the vector between %MuzzlePoint and %endpoint, normalize and scale it appropriately, and use that as the second argument.
let us know if this works.
#15
09/14/2006 (10:58 am)
Oh the damage function does exist, it's just not being used properly. You don't want to use applyDamage, because damage has additional callback you'll probably want. The proper format for the damage function here would be:%nearID.damage(%obj,getWords(%ray, 1, 3),30,"Taser");
#16
BTW: I'm wondering what all the paramaters to .damage() are and what all the words to the raycast are, if anyone knows please respond, but don't go out of your way. My initial problem was fixed so again, thx! :)
09/14/2006 (2:09 pm)
I haven't tried this yet, but I'm guessing it will work. Thx for all the helpBTW: I'm wondering what all the paramaters to .damage() are and what all the words to the raycast are, if anyone knows please respond, but don't go out of your way. My initial problem was fixed so again, thx! :)
#17
BTW: I'm wondering what all the paramaters to .damage() are and what all the words to the raycast are, if anyone knows please respond, but don't go out of your way. My initial problem was fixed so again, thx! :)
Edit:It works! But not my impulse, but that's a whole new problem (expect another thread if I can't find it).
09/14/2006 (2:41 pm)
I haven't tried this yet, but I'm guessing it will work. Thx for all the helpBTW: I'm wondering what all the paramaters to .damage() are and what all the words to the raycast are, if anyone knows please respond, but don't go out of your way. My initial problem was fixed so again, thx! :)
Edit:It works! But not my impulse, but that's a whole new problem (expect another thread if I can't find it).
Torque Owner Cinder Games
%forwardVec = %obj. getForwardVector();
that will return something like.... "1 0 0"
and then you scale is by 5.... and it'l be "5 0 0"
all fine and dandy..
so now you're gonna do a ray cast from the muzzle vec, again, i'll make something up.
"124 -25 2"
I'm guessing that muzzle vect returns a position. i'm not certain. It may simply return a direction?
So you ray cast is gonna go from
"124 -25 2" to "5 0 0"
Clearly that's not what you want.
The problem is you're not taking into account the player position.
Your end point needs to be something like this...
%endPoint = VectorAdd( VectorScale(%forwardVec,5) , %obj.getposition() );
But again I'm not certain what the getmuzzlevec is returning. try adding in some echos to see what values are being passed.