Projectile going of course?

I tried making a projectile script using body velocity and tools but for some reason it keeps going of-course and acting wierd

Local Script in the tool:

local Tool = script.Parent

local Folder = game.ReplicatedStorage:WaitForChild("Pistol")

local Bullet = Folder.Bullet

local Cooldown = false

local DebrisService = game:GetService("Debris")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Tool.Activated:Connect(function()
	
	if Cooldown == true then
		return true
	else
		Cooldown = true
			
		local NewBullet = Bullet:Clone()
		NewBullet.Parent = Tool
		NewBullet.Position = Tool.Handle.Position

		local Velocity = Instance.new("BodyVelocity")
		Velocity.Parent = NewBullet
		Velocity.Velocity = Mouse.UnitRay.Direction * 100

		DebrisService:AddItem(NewBullet, 10)
		
		task.wait(1)
		Cooldown = false
	end
end)

Example:
robloxapp-20240626-2025417.wmv (731.6 KB)

Is there anyway to fix this or perhaps another way to redo the script. I thought of raycasts but i couldnt really figure how to implement it.

I messed up my other reply. I meant to say you can just make the projectile start from the camera, or you can calculate the direction from the tool handle and mouse.Hit.Position. The issue comes from making the velocity relative to the camera, but the starting position relative to the tool.

1 Like

EDIT: i figured it out

new script:

local Tool = script.Parent

local Folder = game.ReplicatedStorage:WaitForChild("Pistol")

local Bullet = Folder.Bullet

local Cooldown = false

local DebrisService = game:GetService("Debris")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Tool.Activated:Connect(function()
	
	if Cooldown == true then
		return true
	else
		Cooldown = true
			
		local NewBullet = Bullet:Clone()
		NewBullet.Parent = Tool
		NewBullet.Position = Tool.Handle.Position

		local Velocity = Instance.new("BodyVelocity")
		Velocity.Parent = NewBullet
		Velocity.Velocity = (Mouse.Hit.Position - Tool.Handle.Position).Unit * 100

		DebrisService:AddItem(NewBullet, 10)
		
		task.wait(1)
		Cooldown = false
	end
end)

and it works!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.