I want to make a swep that fires ragdolls, but I am a complete beginner at Lua.
Knowing nought about Lua I decided to use this fruit launcher I found on gmod.org as a base. Is it as simple as changing the model to a ragdoll?
[code]if (SERVER) then
AddCSLuaFile ("shared.lua");
SWEP.Weight = 5;
SWEP.AutoSwitchTo = false;
SWEP.AutoSwitchFrom = false;
end
if (CLIENT) then
SWEP.PrintName = "Fruit Launcher";
SWEP.Slot = 3;
SWEP.SlotPos = 1;
SWEP.DrawAmmo = false;
SWEP.DrawCrosshair = false;
end
SWEP.Author = "Waffle";
SWEP.Contact = "";
SWEP.Purpose = "";
SWEP.Instructions = "";
SWEP.Category = "Prop Launchers"
SWEP.Spawnable = true;
SWEP.AdminSpawnable = true;
SWEP.ViewModel = "models/weapons/v_rpg.mdl";
SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl";
SWEP.Primary.ClipSize = -1;
SWEP.Primary.DefaultClip = -1;
SWEP.Primary.Automatic = false;
SWEP.Primary.Ammo = "none";
SWEP.Secondary.ClipSize = -1;
SWEP.Secondary.DefaultClip = -1;
SWEP.Secondary.Automatic = false;
SWEP.Secondary.Ammo = "none";
local ShootSound = Sound ("Metal.SawbladeStick");
function SWEP:Reload()
end
function SWEP:Think()
end
function SWEP:throw_attack (model_file)
local tr = self.Owner:GetEyeTrace();
self.Weapon:EmitSound (ShootSound);
self.BaseClass.ShootEffects (self);
if (!SERVER) then return end;
local ent = ents.Create ("prop_physics");
ent:SetModel (model_file);
ent:SetPos (self.Owner:EyePos() + (self.Owner:GetAimVector() * 16));
ent:SetAngles (self.Owner:EyeAngles());
ent:Spawn();
local phys = ent:GetPhysicsObject();
local shot_length = tr.HitPos:Length();
phys:ApplyForceCenter (self.Owner:GetAimVector():GetNormalized() * math.pow(shot_length, 3));
cleanup.Add (self.Owner, "props", ent);
undo.Create ("Launched fruit");
undo.AddEntity (ent);
undo.SetPlayer (self.Owner);
undo.Finish();
end
function SWEP:PrimaryAttack()
self:throw_attack ("models/props/cs_italy/orange.mdl");
end
function SWEP:SecondaryAttack()
self:throw_attack ("models/prop[/code]
Create a prop_ragdoll rather than a prop_physics. Aside from that, yes.
Thanks.
How can I increase the velocity at which the ragdoll flies out of the swep?
phys:ApplyForceCenter (self.Owner:GetAimVector():GetNormalized() * math.pow(shot_length, 3));
Does that have anything to do with it?
nudgings
[QUOTE=ferrus;16780203]nudgings[/QUOTE]
is that something like a bump?
lol. It is when his thing is to far down no one is going to read it. Therefore he nudges it up.
I think so yes.
(to his question)
Still can't get it to fire with more force. Basically what I want is for it to fire at such high velocity that the ragdoll has a seizure.
:D
anyone?
It's pretty straight forward.
do a generic for looping through all the bones of the created ragdoll and apply a force to each one. Make sure you start iterating at 0, not 1 too.
Slightly related code:
[lua]
for i=0, target:GetPhysicsObjectCount( ) -1 do
local physBone = target:GetPhysicsObjectNum( i )
if (physBone:IsValid()) then
--Apply the force here to physBone
end
end
[/lua]
Any chance you could edit this for me? I really don't know how to do it.
[lua]
if (SERVER) then
AddCSLuaFile ("shared.lua");
SWEP.Weight = 5;
SWEP.AutoSwitchTo = false;
SWEP.AutoSwitchFrom = false;
end
if (CLIENT) then
SWEP.PrintName = "Fruit Launcher";
SWEP.Slot = 3;
SWEP.SlotPos = 1;
SWEP.DrawAmmo = false;
SWEP.DrawCrosshair = false;
end
SWEP.Author = "Waffle";
SWEP.Contact = "";
SWEP.Purpose = "";
SWEP.Instructions = "";
SWEP.Category = "Prop Launchers"
SWEP.Spawnable = true;
SWEP.AdminSpawnable = true;
SWEP.ViewModel = "models/weapons/v_rpg.mdl";
SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl";
SWEP.Primary.ClipSize = -1;
SWEP.Primary.DefaultClip = -1;
SWEP.Primary.Automatic = false;
SWEP.Primary.Ammo = "none";
SWEP.Secondary.ClipSize = -1;
SWEP.Secondary.DefaultClip = -1;
SWEP.Secondary.Automatic = false;
SWEP.Secondary.Ammo = "none";
local ShootSound = Sound ("Friends/message.wav");
function SWEP:Reload()
end
function SWEP:Think()
end
function SWEP:throw_attack (model_file)
local tr = self.Owner:GetEyeTrace();
self.Weapon:EmitSound (ShootSound);
self.BaseClass.ShootEffects (self);
if (!SERVER) then return end;
local ent = ents.Create ("prop_ragdoll");
ent:SetModel (model_file);
ent:SetPos (self.Owner:EyePos() + (self.Owner:GetAimVector() * 16));
ent:SetAngles (self.Owner:EyeAngles());
ent:Spawn();
local phys = ent:GetPhysicsObject();
local shot_length = tr.HitPos:Length();
phys:ApplyForceCenter (self.Owner:GetAimVector():GetNormalized() * math.pow(shot_length, 3));
cleanup.Add (self.Owner, "props", ent);
undo.Create ("Launched Ragdoll");
undo.AddEntity (ent);
undo.SetPlayer (self.Owner);
undo.Finish();
end
function SWEP:PrimaryAttack()
self:throw_attack ("models/player/zombiefast.mdl");
end
function SWEP:SecondaryAttack()
self:throw_attack ("models/player/police.mdl");
end
[/lua]
As I said - complete beginner at Lua.
Of course. I reformatted your code so it's also easier to read, it will still run if it's formatted differently, it should be a habbit you get into.
Untested like. But I added a few comments in there so you understand what's going on and what you might want to to (the "TODO")
[lua]
function SWEP:throw_attack (model_file)
local tr = self.Owner:GetEyeTrace();
self.Weapon:EmitSound (ShootSound);
self.BaseClass.ShootEffects (self);
if (!SERVER) then return end;
local ent = ents.Create ("prop_ragdoll");
ent:SetModel (model_file);
ent:SetPos (self.Owner:EyePos() + (self.Owner:GetAimVector() * 16));
ent:SetAngles (self.Owner:EyeAngles());
ent:Spawn();
--local phys = ent:GetPhysicsObject();
local shot_length = tr.HitPos:Length();
--TODO: Try apply force to a few specific parts of the body. As this code will 'throw' the
-- ragdoll in the same pose as it was created IIRC.
-- Forgot how weights of the bone interact.
for i=0, ent:GetPhysicsObjectCount( ) -1 do --Iterate from 0, as there is a bone numbered 0.
local physBone = ent:GetPhysicsObjectNum( i ) --Select/Declare that bones physics object.
if (physBone:IsValid()) then --Just in case.
--Apply the force here to physBone
physBone:ApplyForceCenter( self.Owner:GetAimVector():GetNormalized() * math.pow( shot_length, 3 ) );
end
end
cleanup.Add (self.Owner, "props", ent);
undo.Create ("Launched Ragdoll");
undo.AddEntity (ent);
undo.SetPlayer (self.Owner);
undo.Finish();
end
[/lua]
Thanks! So is the 'shot_length' the force applied? i.e changing it to 4 from 3 = more force?
I like that math function name "Math.pow"
Sounds like its from a Jappaneese Kung-Fu movie.
Sorry, you need to Log In to post a reply to this thread.