How would I go about making a smooth projectile-moving animation?

  1. What do you want to achieve? Keep it simple and clear!
    I want to make my fireball maintain a consistent speed when it moves, and I want to see if there’s a better way of moving projectiles.

  2. What is the issue? Include screenshots / videos if possible!
    My fireball script works so far, but not exactly how I expected it to.
    When I fire the projectile, the speed varies on where my mouse is, and that’s not really what I was aiming for. I wanted the speed to be consistent, and just make the projectile go in the direction of where my mouse is but have a set velocity.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’m new to the DevHub and so I might’ve navigated it incorrectly, but I didn’t find any solutions on the DevHub. I tried using CFrame and bodymovers, but I still had the same result.

Here’s my localscript first:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Mouse = Player:GetMouse()
local MagicEvent = ReplicatedStorage:WaitForChild("MagicFired")

local InputService = game:GetService("UserInputService")
local Debounce = false

InputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.Q then
		MagicEvent:FireServer(Mouse.Hit, Mouse.Hit.Position)
	end
end)

Here’s my serverscript:

local RepStor = game:GetService("ReplicatedStorage")
local MagicEvent = RepStor:WaitForChild("MagicFired")
local Tweenservice = game:GetService("TweenService")

local EaseStyle = Enum.EasingStyle.Linear
local EaseDirection = Enum.EasingDirection.InOut

MagicEvent.OnServerEvent:Connect(function(Player, MouseHit, MouseHitP)
	local Fireball = RepStor:WaitForChild("Fireball")
	local Character = Player.Character
	local HRT = Character:WaitForChild("HumanoidRootPart")
	
	local FireballClone = Fireball:Clone()
	FireballClone.Parent = workspace
	FireballClone.CFrame = HRT.CFrame
	
	local TweenInformation = TweenInfo.new(2, EaseStyle, EaseDirection)
	local TweenGoals = {Position = MouseHitP}
	
	local TweenCreation = Tweenservice:Create(FireballClone, TweenInformation, TweenGoals)
	TweenCreation:Play()
	
end)

Basically, I just want to know if there’s a way to make my fireball move at a certain speed and not depend on my mouse’s position.

3 Likes
local TweenInformation = TweenInfo.new(2, EaseStyle, EaseDirection)
local TweenGoals = {Position = MouseHitP}

The reason the speed is varying is because the time for the tween to complete is fixed at 2 seconds, hence if you fire it near your player it will move slowly and when you fire it farther it will be faster.

To make them move at a fixed speed you would want to use time = distance/speed

To calculate the distance we can get the Magnitude between the initial position of the Fireball, and its final/destination postion (MouseHitP).

local fireballSpeed = 5 -- test and increase/decrease this to fit accordingly
local distanceToTravel = (FireballClone.Position - MouseHitP).Magnitude
local timeToTravel = distanceToTravel/fireballSpeed 

local TweenInformation = TweenInfo.new(timeToTravel, EaseStyle, EaseDirection)
local TweenGoals = {Position = MouseHitP}

Hope this helps.

4 Likes

Thank you so much! I’m new-ish to mathy stuff in coding, but I’ll try to experiment with the code and understand what you did.

1 Like

How would I make a smooth tween like this but incorporating gravity on the projectile?

i would rotate the CFrame every frame