Hi all,
In a custom SWEP primary fire function I create an entity, set its angle to match the owner’s aim vector, and adjust it’s position to be outside of the player model. Once I Spawn the entity, I set it’s velocity to shoot out from the player’s sight.
In my game it spawns in the correct position with the correct angle. However, the velocity is not applied to the entity on the “client side”. In fact, the object is in a completely different position because the velocity did not sync. I printed both values to the screen to show this:
In the above image, the first position is on the server and the second position is on the client. I am guessing that the order in which I am calling these functions is wrong. Would anyone be able to point my in the right direction of the correct way to spawn an entity and set the velocity from the server-side? Here is the code:
Spawn Code
function SWEP:PrimaryAttack()
if not self:CanPrimaryAttack() then return end
if SERVER then
ent = ents.Create("custom_ball")
ent:SetAngles(self.Owner:GetAimVector():Angle())
ent:SetPos(self.Owner:EyePos() + self.Owner:GetAimVector() * 30)
ent:Spawn()
ent:Activate()
ent:GetPhysicsObject():SetVelocity(ent:GetAngles():Forward() * 1000)
self:Remove()
end
end
custom_ball.lua
ENT.Type = "anim"
ENT.PrintName = "Custom Ball"
ENT.Author = "Cruben"
function ENT:Initialize()
self:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
self:PhysicsInit( SOLID_VPHYSICS ) -- Make us work with physics,
self:SetMoveType( MOVETYPE_VPHYSICS ) -- after all, gmod is a physics
self:SetSolid( SOLID_VPHYSICS ) -- Toolbox
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
phys:SetMaterial("gmod_bouncy")
end
end
I am running a 4-player server hosted in my Gmod application (NOT dedicated). Let me know if you need any additional info to help out. Thanks!