How can I keep the velocity same for Tweens?

I’m making a gun and this is for bullet animation, with the current code, it looks very good. However if I shoot on the ground, bullet moves so slowly. I basically want to change the time instead of the velocity in this tween.

local function createTween(start,ending, part)
	part.Position = start
	local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(part, tweenInfo, {Position = ending})
	tween:Play()
end

I tried making some kind of velocity and put that as time but it didn’t really work very well.

1 Like

To change the time of the tween, alter the first number in TweenInfo. You currently have it set to 0.3 seconds, change that to however long you want the tween to take to finish.

I already know tweens. You probably didn’t read correctly. I tried making a velocity and calculating time with it myself but I couldn’t do it and I’m asking if someone can help me.

I see. I initially though you were wondering how to change the time value of a tween.

Create a speed value for each bullet type, or use a standard speed (i.e. 500 studs/s), the speed value should be an integer. This will represent the velocity of the bullet.

Then, calculate the distance between the start and end using (start-ending).Magnitude We will use this to calculate the speed of the animation by using the equation time = distance/velocity

So, divide the distance by the speed and you will get what you are looking for.

Here’s the finished code:

local function createTween(start,ending, part)
	part.Position = start
	local tweenInfo = TweenInfo.new((start-ending).Magnitude/500, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(part, tweenInfo, {Position = ending})
	tween:Play()
end

Change 500 to whatever you want the speed to be. (in studs/second)

2 Likes
local function createTween(start,ending, part)
	part.Position = start
	local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(part, tweenInfo, {Position = ending})
	tween:Play()
end

Its because you’re using the quad easing style, changing the easing style to linear should fix it:

local function createTween(start,ending, part)
	part.Position = start
	local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Linear) -- shorter yet still gets the same thing
	local tween = tweenService:Create(part, tweenInfo, {Position = ending})
	tween:Play()
end

The quad easing style does not cause any issues and is simply a matter of how you want the tween to look.

The amount of time the tween will take is still 0.3 seconds and, thus, when you shoot at the ground, it will still take 0.3 seconds to cover what is ~ 3 studs, and will still be slow.

Basically, this will not work.

Speed = Distance / Time

When tweening something we need the time so let’s rearrange the equation to make time the subject.

Time = Distance / Speed

To find the distance you can cast a ray from the gun and get the distance that way. If the ray does not hit anything set it to the max range of the gun.

Speed is something you will have to play with to get something that feels right.

1 Like

What I did was similar to this but mine was completely wrong I guess. Thanks.

Yeah, no problem. I’d appreciate it if you accepted the solution if it works out for you! :slight_smile:

I already did, not sure if it showed up or not.

1 Like

Didn’t get a notification for it… hmm :thinking: Oh well. If you need anything else please let me know :wink:

1 Like

I just tested again, changed the speed but it looks like if I shoot too far away, bullet becomes so slow. It’s fine for close distances.

This is a matter of changing the speed. Try altering the value which is 500, since this is what determines the speed of the gun.

See:

Please read carefully. I told that I tried to change the speed.

Edit: Wait a min, I think I know why.

This should still just be a matter of speed, and if it is not then it either has to do with your setup of TweenInfo, or other code which is not present. You could try doing what was shown above (though for reasons outside of this question),

Otherwise, the problem has to do with code outside of what has been posted.
The answer to your question still remains the same. And the solution is as I posted.

My bad… Quad was causing it. It’s solved now.

1 Like