Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I use my RayCast with velocity?

What am I trying to do? I'm trying to make my RayCast have bodyvelocity but for that, I need velocity inside the bodyvelocity but for my gun I need to figure out how to attach the velocity to move the direction that I shoot my gun. Here's a part of my script if anyone can understand better from that

local Velocity = Instance.new("BodyVelocity")
Velocity.velocity = Parent.Barrel * 300
Velocity.P = 2300
Velocity.Parent = RayCast

1 comment

0

Um, what? A raycast isn't an object, it can't have velocity. In this case, just don't use raycasts and shoot out a normal bullet.

1 answer

You can't, the best thing you can do is include a wait and have dropoff calculated in the local script. then transfer it all to the main script with the remote event. If you want a bullet to travel along a path with raycast you need to use tweening, I'll provide an example from my raycast pistol main script below:

script.Parent.BulletEvent.OnServerEvent:Connect(function(Player, FromP, ToP, BulletPos)  --FromP is the position from which the bullet ray will be fired, ToP is mouse's CFrame + the calculated spread, BulletPos is position from which the visualized bullet will be fired, it's included here as I have an first person arms script which is local, if I didn't get the local position of it it'd fire from the wrong looking place
if script.Parent.AbleToShoot.Value == true then
	if first == true then
	table.insert(tab, Player.Character)
	first = false
	end
 local RayCast = Ray.new(FromP,(ToP-FromP).unit*500)
 Part,Position,Normal = game.Workspace:FindPartOnRayWithIgnoreList(RayCast,tab,false,true)
 local Dist = (ToP-FromP).magnitude
 if not Dist then Dist = 300 end
local projectile = script.Parent.Bullet.Projectile:Clone()
projectile.Parent = game.Workspace
projectile.CFrame = CFrame.new(BulletPos, ToP)
projectile.Tracer.Enabled = true
 Hole = game.ReplicatedStorage:FindFirstChild("GlockBulletHole"):Clone()
-------------------------------------------------------------------
local Info = TweenInfo.new(
	(ToP-script.Parent.Parts.BE.Position).magnitude/457,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)


local Goals  =
{
	Position = ToP
}

local BulletTween = TweenService:Create(projectile, Info, Goals)
BulletTween:Play()

if (ToP-script.Parent.Parts.BE.Position).magnitude <= 1000 then
game.Debris:AddItem(projectile, (ToP-script.Parent.Parts.BE.Position).magnitude/457)
elseif (ToP-script.Parent.Parts.BE.Position).magnitude > 1000 then
game.Debris:AddItem(projectile, 2.2)
end
end
-------------------------------------------------------------------
wait((ToP-script.Parent.Parts.BE.Position).magnitude/457)
if Part ~= nil then  --this goes on for 223 lines but I'll stop here

This discussion is preserved as it was. New answers, comments, and votes are closed.