Recently I have been having the same issue you are facing because there are so many different ways of moving projectiles. I initially though about handling the movement in a for
or while
loop but I quickly denied that option because I was worried about performance. I was also worried about the projectile not moving smoothly enough.
After reading a post about making a train move I saw that the train was being moved by TweenService. This made me curious about whether this theory would be any good with projectiles. I then had ago using TweenService to create a simple projectile and it turned out nicely in my quick tests.
Making a projectiles with TweenService:
Keep in mind that I haven’t tested this method extensively so I don’t know how well it will perform in a production game. If anyone has any better methods or disadvantages of this method please let me know because I am still testing out this method.
We first need to be able to calculate the time the tween takes. This can be done by using the speed = distance / time triangle @Blokhampster34 touched in earlier. Here is what the triangle looks like.
This triangle gives the formulas for calculating speed, distance and time as shown below:
Speed = Distance / Time
Distance = Speed * Time
Time = Distance / Speed
The formula we are most concerned about is the time formula because one of the parameters in TweenInfo is the time the tween takes to complete. According to the formula for calculating time we need to know the speed and distance. You can find the distance by finding the magnitude between the start and end position and you determine the speed. Here is a little code sample demonstrating this:
local Distance = (StartPosition - EndPosition).Magnitude -- The distance
local Speed = 20 -- The speed you want the projectile to move
local Time = Distance / Speed -- The time the tween takes
With that out the way I can show you a very basic example of the full code because the rest is self explanatory:
local TweenService = game:GetService("TweenService")
local StartPosition = Vector3.new(0, 0, 0)
local EndPosition = Vector3.new(10, 10, 10)
local Part = Instance.new("Part") -- Creates a new part
Part.Anchored = true
Part.Size = Vector3.new(1, 1, 1)
Part.Position = StartPosition
Part.Parent = workspace
local Distance = (StartPosition - EndPosition).Magnitude -- The distance
local Speed = 20 -- The speed you want the projectile to move
local Time = Distance / Speed -- The time the tween takes
local tweenInfo = TweenInfo.new(Time, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 1, false, 5)
local Tween = TweenService:Create(Part, tweenInfo, {Position = EndPosition})
Tween:Play()
This code is obviously not complete because for one the projectile doesn’t face the way it is moving.
I haven’t tried out using BodyMovers to make projectiles so I am not one hundred percent sure of a way to make a projectile using them. However I am fairly confident you will need to use the triangle I shown above in some way.