What do you want to achieve?
I would like to know how I would utilize raycasting in order to make a gun,
What is the issue? I
I know how to use raycasting, however wouldn’t the ray instantly detect the part once the ray is cast - how would I make the ray only detect a part once the bullet hits it rather than instantly dealing damage?
What solutions have you tried so far?
I haven’t found any solutions to this problem so far.
I currently have a local script that clones a bullet part on the client and then tweens it to the mouse position. How would I combine this with raycasting?
local script:
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()
local Range = 800
Mouse.Button1Down:Connect(function()
local MouseHit = Mouse.Hit
local Origin = Char.Head
local Distance = (Origin.Position - MouseHit.Position).Unit * Range
local Bullet = game:GetService("ReplicatedStorage").PartBul:Clone()
Bullet.Parent = game.Workspace
Bullet.CFrame = Char.Head.CFrame
game:GetService("Debris"):AddItem(Bullet, 4)
local Time = ((Origin.Position - Distance).Magnitude/30)
local Ray1 = Ray.new(Origin.Position, (Origin.Position - (Distance)))
local T = game:GetService("TweenService"):Create(Bullet, TweenInfo.new(Time), {Position = Origin.Position - (Distance) })
T:Play()
end)
One possible way might be to set the speed your bullets will travel and then calculate the time based on the distance it needs to go. Your tween could then be used to move the projectile from the gun to his target position over the span of that calculated time. For a hit. you could watch for a touch event on the bullet and check for when it touches something it could damage.
I’ve heard people say this as well. But its been reliable enough for me so far and the alternative is a lot more complex. You need someway to detect when the two parts intersect in world space.
If I wasn’t going to use touch, I think it would end up being something like raycasting mixed with region3 stuff and crap load of calculations, perhaps even requiring a polling loop. Event based is generally preferred over polling for performance I believe.
What if you took the bullet velocity and the distance it had to travel and just wait the time it would take for a bullet to travel that distance before checking for parts on the ray?
If you want the bullet to have travel time you just need to break the path into multiple steps
local RunService = game:GetService("RunService")
function FireBullet(origin, direction, speed, ignore, callback)
local stepped
stepped = RunService.Stepped:Connect(
function(deltaTime)
local hit, hitpos, normal = workspace:FindPartOnRayWithIgnoreList(
Ray.new(origin, direction.Unit*deltaTime*speed),
ignore
)
if hit then
callback(hit, hitpos, norm)
stepped:Disconnect()
else
origin = hitpos
end
end
)
end
I recommend just having bullets be instant however because it feels more satisfying. If you want to have a weapon where tracking is required use physical projectiles.
Touched registers instantly on the client who controls a projectile (as per BasePart:SetNetworkOwner) The Touched event is sent from the client to the server which has a delay. If you can have the client tell you where the collision occurred and then put some checks on the projectile to make sure that what the client claims is reasonable then you can end up with something pretty reliable.