How do I tween a bullet effectively?

Hello everyone!

Currently I’m working on a shotgun. Instead of making it “laser bullets”, I used twening to tween the bullets (Despite bullet physics, drops etc.)
It tweens between the Shooting Part and pos, which is a Vector3 determined by Raycasting.

                                local tweenInfo = TweenInfo.new(
                                            	0.3, 
                                              	Enum.EasingStyle.Linear, 
                                               	Enum.EasingDirection.Out, 
                                                0, 
                                            	false,
                                                0
                                               )

                            local goal = {
                                       Position = pos	
                                         }

							local tween = tweenser:Create(BulletTrail, tweenInfo,goal)		
							tween:Play()

Here’s the current result when the range is set to 50 studs. (Expected result)
https://gyazo.com/f82c960c2f777085c5da19a4aed61fb9

However, if I increase the range of the shotgun (E.g. 200), it tweens really fast because the goal is more farther:
https://gyazo.com/97ecf636a20c40a4852e0cce41a6d905

Is there any way to control the tweening speed the same while the tweening goal is different/farther?

4 Likes

I wouldn’t say that using Tweening for a bullet system will be the most fitting or realistic just because of the nature of Tweening itself, no physics involved.

However, just to answer your question, is it possible that you define your shooting speed or maintain it? What I meant by that is, if a bullet travels 50 studs in 0.3 seconds. How can you make it travel 50 * 4 studs in 0.3 * 4 seconds?

1 Like

If your bullet speed is 1000 studs/sec, and your target is 300 studs away, then you should tween for 0.3 seconds (since 300/1000 is 0.3). Likewise, when you double the distance to 600, you should tween for twice as long (0.6 seconds), since 600/1000 is 0.6

1 Like

It looks like the bullet tweening is just for visual feedback and not for actual calculations so this should work fine.

1 Like

So imagine you have this formula for how far you want the bullet to go per second:

50 (studs) / 0.3 (seconds)

You would want to create another ratio for the new distance, but the ratio between studs and seconds will be the same:

d (studs) / s (seconds) == 50 / 0.3

Figuring out d should be simple enough:

d = (BulletTrail.Position - pos).Magnitude

Now, we can solve for s using simple algebra:

d / s == 50 / 0.3
s / d == 0.3 / 50
d * (s / d) == d * (0.3 / 50) -- d now cancels out on the left side,
s = d * 0.3 / 50

If you apply this math to your TweenInfo, it should now look something like this:

local tweenInfo = TweenInfo.new(
	(BulletTrail.Position - pos).Magnitude * 0.3 / 50,
	Enum.EasingStyle.Linear,
	--rest of TweenInfo
)
-- keep everything else the same.

Keep in mind that you will need to create a new TweenInfo every shot, but that shouldn’t be very expensive, and you were probably already doing this in the first place by the looks of it!

14 Likes

Never considered maths! Thanks :slight_smile:

1 Like

Tweening should be the best way, apart from tweening, I can’t think of a better way :thinking:

1 Like

Heard this gun uses ray casting and such and its pretty performance-wise, good and functions pretty well?

Tried searching to see if it uses tweening but instead the fastcast gun uses cframing. Don’t really know which one is best performance wise.

Stress-test done with it to show that it performs pretty well even on heavy load: