Hi, I’m making a gun with ray-casting, it seems to be not working properly. Here’s the code:
Client-Side;
local Spas = script.Parent
local BulletPlace = Spas:WaitForChild("BulletPlace")
Spas.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local MouseRay = Mouse.UnitRay
game.ReplicatedStorage.Shoot:FireServer(MouseRay,Mouse, BulletPlace)
print("Shot!")
end)
end)
Server-Side;
game.ReplicatedStorage.Shoot.OnServerEvent:Connect(function(Player, Mouseray, mouse, bulletShoot)
local ray = Ray.new(bulletShoot.Position, Mouseray.Direction * 100)
local hit, position = game.Workspace:FindPartOnRay(ray, nil)
local bullet = game.ServerStorage.Bullet:Clone()
bullet.CFrame = bulletShoot.CFrame
bullet.Parent = game.Workspace
local bv = Instance.new("BodyVelocity", bullet)
bv.Velocity = position
end)
Help is appreciated, thanks!
1 Like
If you use BodyVelocity
for a gun, you shouldn’t use RayCasting
for it.
1 Like
Then…? what should I use if no body velocity
1 Like
You should use BodyVelocity
, but using RayCasting
for the Velocity
p. itself isn’t a so-good idea.
But what do I use now? Do you know what to use?
1 Like
Use Ray.Unit beacuse you already created a Ray
.
Read about it here.
What do you mean ray.Unit? I’m not sure what you mean
Read the article I shared in the message above. 
Uhh bro there’s no link in the message you sent above.
Now I see. thanks for telling me that I should use Unit.
Np. Have a good day
(30 chars)
Does the unit have LookVector? I think so
1 Like
No. It does not have a LookVector
.
Oh, well, this is the code I wrote for now.
game.ReplicatedStorage.Shoot.OnServerEvent:Connect(function(Player, bulletstart, MousePos)
local ray = Ray.new(bulletstart.Position, (MousePos - bulletstart.Position).Unit)
local hit, pos = game.Workspace:FindPartOnRay(ray , nil)
local bullet = game.ServerStorage.Bullet:Clone()
bullet.Parent = game.Workspace
bullet.Position = pos
print(pos)
end)
Not working
You can not define the Ray
as Ray.Unit
, define the Ray
as a normal Ray
and then later when you create the BodyVelocity
set the Velocity
to be equal to Ray.Unit
You mean local Ray = Ray.new()
No, for instance:
local ray = Ray.new(Vector3.new(0, 5, 0), Vector3.new(0, 15, 0))
Will my code look like this? local ray = Ray.new(bulletStart.Position, (bulletStart.Position - mouse.Hit.p).Unit)
You define a Ray Unit there. Define a normal ray, as you did before.