Making a bullet look like it's moving

Hello everyone, this is my first post, so please tell me if I did something wrong.

Right now, I have a simple Raycast gun that casts a single ray toward the direction of the mouse.

The bullet is just a straight line that starts at the gun hole to the hit position of the ray. Here is my code for that portion:

local ray = Ray.new(startPosition, direction*2000)
local hit, position, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray,ignore)
if hit and hit.Parent then
	OnHit(plr,hit,position,normal,damage)
end

However, I’m not sure how to make the bullet look like it’s moving instead of it just being a stationary line. So far, I’ve tried tween service, BodyVelocity, Velocity, and they all seem to make my game lag a lot(especially when firing guns with a fast fire rate). Is there any way to add that extra effect of bullet movement without lag?

Any help is appreciated!

You can go about using a beam, that will have no lag and it’s just a moving texture.

You have to make a new attachment at the position of the mouse’s hit. and then make a new beam instance and connect it with an attachment at the gun’s barrel/hole. Now you have to mess around with the settings of the beam to set it’s texture speed and such, but i hope you can figure out the rest!

Yeah I actually already am using a beam, but for some reason tweening the beam still causes lag.

Wait I think I get what you mean. I will try that thanks!

1 Like

Do you run the tween via the client?

Oh, no it’s in a server script

I recently switched over to rendering the bullet client side, so I will try to tween it there instead.

Tweening on the client is the way to go, when tweeining on the client there is a lot less lag on the server and for the actual local player. Let me know how it goes!

Ok, thank you for the help! I will try that.

That’s a nice idea, but we should take into account that the other players won’t be able to see the trail.
so, yeah it has an advantage and the disadvantage and so it depends upon the type of game i guess.

Well, can’t I render the tween on other clients as well?

You can use Velocity

Useful links
- YouTube ( By Explicit Username )
Advanced Roblox Scripting Tutorial #24 - Raycasting (Beginner to Pro 2019) - YouTube ( By TheDevKing )

These both tutorials will help you. TheDevKing’s is teaching raycasting and making a turret at the end.

Yes, but that would take some decent scripting and knowledge about remote events and functions, i would recommend it’s better to just use a beam since it causes less lag and is way smoother, but it’s always upto you!

Yeah, I have a remote event that fires to all clients, and I’m tweening the beam’s first attachment position.

First of all you should the visualizing of gun in the Client. A remote event should be fired to each client with needed arguments and make the Bullet’s Visualizing in the Client . You can use Beams or BodyVelocity

A tip: You can try using the New type of raycasting.

local OriginRay = Part1.Position --Origin
local GoalRay = Part1.CFrame.LookVector * 20 --Looking forward 20 studs

local Param = RaycastParam.new() --Creating new parameter
Param.FilterType = Enum.RaycastFilter.Whitelist -- Choose one balcklist or whitelist. Blacklist will ignore the ray that hits the filter. While whitelist ignore rest of the instance except the instance inside the filter table
Param.IgnoreWater = true -- Change to false if you not ignore the water
Param.FilterDescendantsInstances = {} --Put instances in the list of filter in the filter type
Param.CollisionGroup = "" --Ignore the id of collision group

local Raycast = game.Workspace:RayCast(OriginRay, GoalRay, Param)

print(Raycast.Instance:GetFullName()) --get result instance name

Resources : New RaycastParams Property, Deprecating Old Raycast Functions and
RaycastResult | Documentation - Roblox Creator Hub

1 Like

I wouldn’t suggest watching DevKing’s Tutorial since he is teaching deprecated Raycasting and not the new one(Its not his fault , when the video was made the new Raycasting was not a thing). I would suggest to read the explanation about new raycasting in DevForum and DevHub.

What are the benefits of the new Raycasting method?

New RaycastParams Property, Deprecating Old Raycast Functions This devforum post explains it well.

I just got it working by using TweenService

local TS = game:GetService("TweenService")
local travelT = (tool.Hole.Position-position).Magnitude/1800
local info = TweenInfo.new(travelT,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0.01)

local goal = {}
goal.Position = bullet.Length.Position
local bulletTween = TS:Create(bullet.Attachment0,info,goal)

bulletTween:Play()
game:GetService("Debris"):AddItem(bullet,travelT)

I’ll definitely look into using workspace:RayCast rather than Ray.new().

Thank you everyone for your help!

2 Likes