Help with bezier curves

Hello there, im trying to make parts curve with bezier curves and move to destination with random spread.

The problem is, because i set the random offset on X axis, when i turn 90 degrees right which makes me match up with the X axis, parts does not spread anymore.

Basically, i know the problem but not the solution.

Videos of what im talking about:

1- How it should work
https://gyazo.com/859cb9f0169c4a2ab85eb5540433e2ff

2-How it works when i turn right
https://gyazo.com/d810b619ba6946abbc92798daa7613ba
As you can see it goes in a straight line.

My code:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, mouseP)
local hrpp = player.Character.HumanoidRootPart.Position
local endp = mouseP


local offset = Vector3.new(math.random(-20,20),math.random(-10,10),0)

local mid = (hrpp + endp)/2 + Vector3.new(0,40,0) + offset

local part = Instance.new("Part",player.Character)
part.Size = Vector3.new(2,2,5)
part.Anchored = true
part.CanCollide = false



for i= 1, 50 do

local t = i/50
local q = quadBezier(t,hrpp,mid,endp)

part.CFrame = CFrame.new(q)
part.CFrame = CFrame.new(q,quadBezier(t + 0.1,hrpp,mid,endp))

wait()

if i == 50 then
part:Destroy()
end

end
end)

Why not try and use the LookVector of the HumanoidRootPart relative to endPos instead of a constant Vector3 for the offset variable?

You can offset it one way or another by using the RightVector. We’re trying to offset by the perpendicular vector from the trajectory:

local offset = CFrame.new(hrpp, endp).RightVector * Vector3.new(math.random(-20,20),math.random(-10,10),0)
1 Like

Hi, sorry for the late reply, i tried what you suggested and nope, it didnt work.

Plus, now Y axis doesnt go random too.

try changing this line for

local hrpc=player.Character.HumanoidRootPart.CFrame
local offset = hrpc.RightVector*math.random(-20,20)+Vector3.new(0,math.random(-10,10),0)

the problem is you’re using setting the offset based on worldspace instead of objectspace so when you turn right in that terms is the “world” 's right side but using RightVector you’re getting the right side of the humanoidrootpart so it should work now.

1 Like

That worked, thank you for the solution.

1 Like