Gun projectile's size isn't right

Alright so I have this gun that I created using Raycasting but the projectile that comes off the gun doesn’t have the same size as the ray that I casted from the gun, it might not be the projectile’s fault but I think that it is, anyways here’s a GIF which shows my issue:

As you can see in the GIF, the gun projectile’s size isn’t what it is intended to be instead it looks weird since it’s supposed to shoot forward 30 studs in the Z axis but it doesn’t and the hinge is for the gun’s front surface I made it like that so it is easier for me to see where the gun’s front surface is.

Here’s the script that I’m using which also includes comments to help you understand what I’m doing that also explains what important lines of the code does which can possibly be the issue:

Code
local tool = script.Parent
local Debris = game:GetService("Debris")
local toolHandle = tool.Handle -- Getting the tool's Handle which is the only part that I'm using to create this gun.
local rayOrigin = toolHandle.CFrame.LookVector -- This is where the ray starts.
local rayDirection = toolHandle.CFrame.LookVector + Vector3.new(0,0,30) -- This is where the ray will go.
local gunRay = Ray.new(rayOrigin,rayDirection) -- Creating the ray.

tool.Activated:Connect(function()
	local thing, hit = workspace:FindPartOnRayWithIgnoreList(gunRay,{toolHandle}) -- Casting the ray.
	
	local part = Instance.new("Part") -- This is where I create the part and modify the properties of the part.
	part.Transparency = 0
	part.Anchored = true
	part.CanCollide = false
	part.Color = Color3.fromRGB(255,0,0)
	part.CFrame = toolHandle.CFrame
	part.Size = Vector3.new(0,0,30) -- Maybe this is the issue?
	part.Parent = workspace
	Debris:AddItem(part,0.2)
end)

The problem is with your rayDirection. You can’t simply add a vector to the CFrame of the part. Think of rayDirection as the “slope” of the ray in the 3D space. Also getting the LookVector of the gun’s CFrame won’t start the ray at the gun.

If you want to shoot a ray 30 studs in front of the gun, you can find the position you want the ray to end, and then subtract the origin position from that.

This should work:

local rayOrigin = toolHandle.Position -- just get position
local rayTarget = (toolHandle.CFrame * CFrame.new(0, 0, -30)).p -- 30 studs relative to orientation of part
local rayDirection = rayTarget - rayOrigin
1 Like

I still don’t understand how this will work, could you explain more in-depth? I tried replacing the projectile’s size with the rayTarget and now it looks like this:

I still don’t understand what I’m supposed to do here.

Hmm there also seems to be a problem with you creating the part to visualize the raycast. Remember that part’s positions are defined by their center, so by setting the part’s CFrame to the tool’s CFrame, the center of the part will be at the tool, not one of the edges.

I did a quick test to see how to properly position the part, and this works:

part.CFrame = toolHandle.CFrame * CFrame.new(0, 0, -15)

Basically I’m just position the part half way down the ray, so the end points will meet up properly.

1 Like