Hello fellow developers! I am interested on how I can create a projectile that comes from the players humanoid root part and goes straight until it hits something, I tried tween service but the touched event didn’t fire. I also know there is a way to do it with body position but I don’t really know how.
Use Raycasts with a very huge direction: HRP.CFrame.LookVector * math.huge
And just tween it to the RayCast.Position
I use a fire and forget method. Basically, when a weapon fires, the projectile is cloned. Then I apply a force to counteract gravity and a force to make it move in the desired direction. The projectile has everything it needs: sounds, models, scripts, etc… Then I let the physics engine take it from there.
The method that @SomeFedoraGuy mentioned works, but I only use something like that for things like guns where a bullet traveling at 1800 studs/sec is a bit much for the engine to handle properly. I use a raycast to see if it hit something. If it does, I weld a bullet onto the target.
This is the code that adds a force to counteract gravity so the projectile will float.
-- Allows the projectile to float.
local function effectFloat(part)
-- New Attachment
local attach = Instance.new("Attachment")
-- Attachment Parameters
attach.Name = "ProjAttach"
attach.Position = Vector3.new(0, 0, 0)
attach.Parent = part
-- New VectorForce
local force = Instance.new("VectorForce")
-- VectorForce Parameters.
force.Name = "FloatForce"
force.Force = Vector3.new(0, part:GetMass() * workspace.Gravity, 0)
force.ApplyAtCenterOfMass = true
force.RelativeTo = Enum.ActuatorRelativeTo.World
force.Attachment0 = attach
force.Parent = part
end
You can do something similar for the movement force. You will need a script that’s parented to the projectile itself to handle any impacts it may encounter.