How to represent Ray if in air with CFrame

I made a spread system for my Shotgun but cant get it to display in the air because it says “CFrame is not a valid member of Vector3”

Ive tried to convert the direction into a Vector3 but I cant think of how to

		local Dir = CFrame.new(BulletStart,mousehit) * CFrame.Angles(math.rad(math.random(-spread, spread)),math.rad(math.random(-spread, spread)),0)
		local Shoot = workspace:Raycast(BulletStart, Dir.LookVector * range, Params)
		if not Shoot then
			endpos = BulletStart + Dir * range
		else
			endpos = Shoot.Position
		end

Maybe try something like this for the general direction:

local direction: Vector3 = (mousehit - BulletStart).Unit

you can then add offsets to this for the spread

I dont know how to offset it since theres no CFrame.Angles equivalent in Vector3 can you point me in the general direction?

Yes, you can imagine a circle whose surface is perpendicular to the axis represented by the general direction and has a radius of spread. The final destination should be anywhere in that circle. I believe this may work:

-- Function to generate a random vector within a given spread
local function randomSpreadVector(spread)
    local randomOffset = Vector3.new(
        math.random() * spread - spread/2,
        math.random() * spread - spread/2,
        math.random() * spread - spread/2
    )
    return randomOffset
end

-- Calculate the general direction
local generalDirection = mousehit - BulletStart

-- Add a random offset within the specified spread
local randomOffset = randomSpreadVector(spread)
local finalDestination = generalDirection + randomOffset

-- Calculate the unit direction vector
local direction = (finalDestination - BulletStart).Unit

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