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?
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!
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!
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.
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!
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
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.
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().