I’m trying to make a pilum that the player can throw, but I can’t quite figure out how to make the spear go in the correct direction. This is my first time making a projectile.
Portion of code that is relevant: (In a module script)
I can’t watch the video (Windows Media Video download meets mac OS), so here is a general explaination.
If you want it to be fully physically accurate, you would just apply an impulse to give it an initial velocity in the CFrame.LookVector direction (or use a line velocity with relative to attachment then delete it after a second), and make sure the tip end is slightly heavier than the other end.
You probably want something that looks nicer to other players though, since most people don’t know how spear arcs really look. For this, do something similar: give the spear an initial velocity in the throw direction, then every heartbeat use an align orientation to align the spear with it’s velocity. This makes the spear face the exactly direction it’s going.
I probably should have asked, what type of spear do you want? Do you want it just to go in a straight line? (If so use a LineVelocity or BodyVelocity with an AlignOrientation or BodyGyro to keep it facing the line velocity direction). If you want an arc, do the above.
Please don’t use video download links if you can avoid it, they’re very sketchy, often don’t work, and are kind of a pain. You could quickly upload to a site like ez gif to get a gif that’s can be embedded.
Wow, thanks for the very very in-depth description. I love it when I learn something new during a project! As for the video, I didn’t take the time to do that cause I was kinda gettin’ rushed out the door to go somewhere. So here’s a better one.
local spear = -- A BasePart of your spear
local alignOrientation = -- create an align orientation with typical settings (OneAttachment mode, set the attachment, max/high force, etc)
local heartbeatConnection = RunService.Heartbeat:Connect(function()
alignOrientation.CFrame = CFrame.LookAt(Vector3.new(0,0,0), spear.AssemblyLinearVelocity, spear.CFrame.UpVector)
end)
-- TODO: Disconnect the heartbeat connection when finished e.g. it hits something or it's destroyed etc