Hi i have a code that plays an animation on primary fire, but it works no correctly. How to fix it?
https://youtu.be/zfefY_6LTeo
function SWEP:PrimaryAttack()
if IsFirstTimePredicted() then
if (CLIENT) then return end
local ply = self:GetOwner()
ply:LagCompensation (true)
local shootpos = ply:GetShootPos()
local endshootpos = shootpos + ply:GetAimVector() * 70
local tmin = Vector(1,1,1) * -10
local tmax = Vector(1,1,1) * 10
local tr = util.TraceHull({
start = shootpos,
endpos = endshootpos,
filter = ply,
mask = MASK_SHOT_HULL,
mins = tmax
})
if(not IsValid(tr.Entity)) then
tr = util.TraceLine({
start = shootpos,
endpos = endshootpos,
filter = ply,
mask = MASK_SHOT_HULL
})
end
local ent = tr.Entity
if (IsValid(ent) && (ent:IsPlayer() || ent:IsNPC())) then
self.Weapon:SendWeaponAnim(ACT_GESTURE_FLINCH_LEFTARM)
self.Owner:SetAnimation( PLAYER_ATTACK1 )
local anim
local randNum = math.random(1, 2)
if randNum == 1 then
anim = "fists_left"
else
anim = "fists_right"
end
local vm = self.Owner:GetViewModel()
vm:SendViewModelMatchingSequence( vm:LookupSequence( anim ) )
ply:EmitSound(HitSound)
ent:SetHealth(ent:Health() - 5)
if (ent:Health()<1) then
ent:Kill()
end
ply:SetHealth(math.Clamp(ply:Health() + 5))
elseif (!IsValid(ent)) then
self.Weapon:SendWeaponAnim(ACT_GESTURE_FLINCH_LEFTARM)
self.Owner:SetAnimation( PLAYER_ATTACK1 )
local anim
local randNum = math.random(1, 2)
if randNum == 1 then
anim = "fists_left"
else
anim = "fists_right"
end
local vm = self.Owner:GetViewModel()
vm:SendViewModelMatchingSequence( vm:LookupSequence( anim ) )
ply:EmitSound(SwingSound)
end
end
local ply = self:GetOwner()
self:SetNextPrimaryFire( CurTime() +0.5)
self:SetNextSecondaryFire( CurTime() +0.6)
ply:LagCompensation (false)
end
function SWEP:Deploy()
local speed = GetConVarNumber( "sv_defaultdeployspeed" )
local vm = self.Owner:GetViewModel()
vm:SendViewModelMatchingSequence( vm:LookupSequence( "fists_draw" ) )
vm:SetPlaybackRate( speed )
self:SetNextPrimaryFire( CurTime() + vm:SequenceDuration() / speed )
self:SetNextSecondaryFire( CurTime() + vm:SequenceDuration() / speed )
return true
end
Somebody ?
SetAnimation and SendViewModelMatchingSequence should be called in prediction - really, I don't see anything in that code that would require you to check IsFirstTimePredicted. The functions listed should also be called shared - you should use a SERVER check only for the SetHealth/Kill calls. Lastly, you can remove the SendWeaponAnim call since it vm:SendViewModelMatchingSequence takes precedence.
THX!!!!
Sorry, you need to Log In to post a reply to this thread.