Projectile tween breaks when going towards the sky

You can write your topic however you want, but you need to answer these questions:
I am trying to make a projectile using the Tween Service. It works but when I attempt to fire the projectiles towards the sky they don’t go at the speed they’re supposed to go and really slow down

Firing towards ground

Firing towards sky

The code for the projectile tweening

local Pos = mouse.Hit.p

local TI = TweenInfo.new(
	(Star.Position - Pos).Magnitude * 0.5 / 50,
	Enum.EasingStyle.Cubic,
	Enum.EasingDirection.In
)
TS:Create(Star,TI,{CFrame = CFrame.new(Pos)}):Play()

If you’re aiming towards the sky. mouse.Hit will be thousands of studs away, but if you’re aiming towards the ground, mouse.Hit will be much closer. The reason the effect isn’t showing while you’re firing towards the sky is because the distance is too large.

If you want to mitigate this, the easiest solution would be to add a maximum distance (so that if you’re firing at the sky, it ends somewhere).

the * 0.5 / 50 is what that is, its a cap on the speed

Let’s say that the .Magnitude is 8,000. 8,000 * 0.5 / 50 (80 studs!) is still incredibly large for your purpose. You’ll need to add an “if” statement with a maximum distance.

i don’t really know how to do that to be honest

No worries. It’s a pretty simple fix. I set the maxTime to 10, but if you want to change it higher or lower, just change the variable to fit your needs.

local Pos = mouse.Hit.p;
local time;
local maxTime = 10;

if ((Star.Position - Pos).Magnitude * 0.5 / 50 > maxTime) then
	time = maxTime;
else
	time = (Star.Position - Pos).Magnitude * 0.5 / 50;
end

local TI = TweenInfo.new(
	time,
	Enum.EasingStyle.Cubic,
	Enum.EasingDirection.In
)
TS:Create(Star,TI,{CFrame = CFrame.new(Pos)}):Play();

Nice style, btw.

when the mouse is pointing at the sky, the position returns nil
add barriers around the baseplate to mitigate this issue

There is definably a better way of doing this, someone else replied with a fix that i will try first

It worked thank you so much.
Also style as in ability effects? those are something I’ve been doing for a while now so I decided I wanted to actually attempt to make something with them

Yeah, that’s what I meant by that, I was saying that they look cool.

1 Like

actually something I just noticed with this is the further away the target is the slower the projectile moves, is that an easy fix?

Let’s say that you want the projectile to move 1 stud per second. If the target is 9 studs away, you’ll multiply that to get 9 seconds and keep the same ratio of time to distance. If that doesn’t work, try messing with the maxTime variable and seeing if that fixes anything.

1 Like

To fix this, I would just use math.clamp() on the distance.

that helped but didn’t completely fix it, but changing the easing from in to linear did completely fix it.
Kind of a shame I think the easing style added a nice touch to the effect

Use ‘Out’ for the easing direction, additionally, as suggested here.

The time should be clamped between two values, such that it isn’t too small nor too large.

local Enumeration = Enum
local Game = game
local TweenService = Game:GetService("TweenService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local Star --Define this.

local Distance = (Mouse.Hit.Position - Star.Position).Magnitude
local Time = math.clamp(Distance / 100, 2, 10) --Use a clamping function to clamp the result between two values.
local Tween = TweenService:Create(Star, TweenInfo.new(Time, Enumeration.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = CFrame.new(Mouse.Hit.Position)})
Tween:Play()

I’ve clamped the value between two and ten although you may need to fiddle with those values until you reach the desired result.