Simple (I hope) question, Making an entity fly in a straight line at a constant velocity?

Hello, I’m trying to make an entity that, when spawned, flies in a straight line using the owners aim as the starting angles.
I can get the spawning and initial angles set right, but I can’t get it to actually get moving without errors.
This is what i have so far:
[lua]

function ENT:Initialize()
local phys = self:GetPhysicsObject()
local ply = self.Owner
local velocity = self.Owner:GetAimVector()
velocity = velocity * 100
self:SetModel( “models/weapons/bullet.mdl” )
self:PhysicsInitSphere( 7, “floatingstandable” )
self:PhysWake()
local phys = self:GetPhysicsObject()
phys:EnableGravity(false)
phys:SetVelocity(velocity)
end
[/lua]

Technically this “works”, it spawns and fires but it throws an error in console and I just can’t fix.


 attempt to call method 'GetAimVector' (a nil value) 

You should do this code in whichever place you’re spawning it instead of Initialize since the owner isn’t set yet.

Hmm… Doing this, it doesnt spit out any errors, but it also just doesnt move
[lua]
function SWEP:PrimaryAttack()
self:SetNextPrimaryFire( CurTime() + 0.1 )

if ( !SERVER ) then return end

local Forward = self.Owner:EyeAngles():Forward()

local ent = ents.Create( "entity_megaman_bullet" )

if ( IsValid( ent ) ) then
	local velocity = self.Owner:GetAimVector()
	self:EmitSound(Sound(self.Primary.Sound))
	ent:SetPos( self.Owner:GetShootPos() + Forward * 32 )
	ent:SetAngles( self.Owner:EyeAngles() )
	ent:Spawn()
	ent:SetOwner( self.Owner )
	velocity = velocity * 1000
	ent:SetVelocity(velocity)
end

end
[/lua]

Can you post your new Initialize code?

[lua]
function ENT:Initialize()
local phys = self:GetPhysicsObject()
local ply = self.Owner
– local velocity = self.Owner:GetAimVector()
– velocity = velocity * 100
self:SetModel( “models/weapons/bullet.mdl” )
self:PhysicsInitSphere( 7, “floatingstandable” )
self:PhysWake()
local phys = self:GetPhysicsObject()
phys:EnableGravity(false)
– phys:SetVelocity(velocity)
end
[/lua]

bumple

This is code directly from my confetti gun (minus things that aren’t needed):

entitities/ent_confetti/init.lua


function ENT:Initialize()

  self:PhysicsInit(MOVETYPE_VPHYSICS)
  self:SetMoveType(MOVETYPE_FLY)
  self:SetSolid(MOVETYPE_NOCLIP)
  self:SetMoveCollide(MOVECOLLIDE_FLY_CUSTOM)
  local phys = self:GetPhysicsObject()

  if phys:IsValid() then
    phys:Wake()
  end

end


weapons/weapon_confet/shared.lua


function SWEP:PrimaryAttack()
  local ply, projectile, vsrc, vang, vvel, vthrow
  ply = self.Owner

  if SERVER then
    if not IsValid(ply) then return end

    vang = ply:GetAimVector()
    vvel = ply:GetVelocity()
    vthrow = vvel + vang * 800
    vsrc = ply:GetPos()

    projectile = ents.Create("ent_confetti")

    if IsValid(projectile) then
      projectile:SetOwner(ply)
      projectile:Spawn()
      projectile:SetPos(vsrc)
      projectile:SetVelocity(vthrow)
    end
  end
end

If you need any help with explanation, leave a comment, this should work, just adjust to suit your needs

Don’t use

Entity:SetVelocity on VPhysics simulated entities (MOVETYPE_VPHYSICS) – use

PhysObj:SetVelocity instead



local phys = ent:GetPhysicsObject()
phys:SetVelocity(velocity)


Alternatively, I believe you can use SetAbsVelocity on the entity and it will translate it to the physobj. Unfortunately, Garry mismatched the engine to Lua bindings for the Set*Velocity methods, so it might be some trial and error to find the proper one.

Why? Literally, why, what difference?

Entity velocity is different than physobj velocity internally. Think of how a ragdoll leg can still move while the rest of the objects aren’t – that’s velocity managed by the physics object versus the entire entity.