Game Development Reference
In-Depth Information
Shooting a projectile
Shooting a bullet projectile ends up with creating a physics capsule, setting the position and
orientation through the Core.SetAxis function, attaching a particle to the bullet, and
then applying an impulse to that bullet:
SoldierAgent.lua :
local function _ShootBullet(sandbox, position, rotation)
local forward = Vector.Rotate(Vector.new(1, 0, 0),
rotation);
local up = Vector.Rotate(Vector.new(0, 1, 0), rotation);
local left = Vector.Rotate(Vector.new(0, 0, -1),
rotation);
-- Create a capsule shaped bullet to launch forward
given the
-- weapons muzzle orientation.
local bullet = Sandbox.CreatePhysicsCapsule(
sandbox, 0.3, 0.01);
Core.SetMass(bullet, 0.1);
Core.SetPosition(bullet, position + forward * 0.2);
Core.SetAxis(bullet, left, -forward, up);
-- Create a particle to visibly show the bullet.
local bulletParticle = Core.CreateParticle(bullet,
"Bullet");
Core.SetRotation(bulletParticle, Vector.new(-90, 0, 0));
-- Instantaneously apply a force in the forward
direction.
Core.ApplyImpulse(bullet, forward * 750);
return bullet;
end
Search WWH ::




Custom Search