Bezier Curve help

Im trying to make bezier curve prorjectile, it keeps giving me an error not sure why

attempt to perform arithmetic (add) on number and Vector3

the code is

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character.Humanoid
local mouse = player:GetMouse()
local energyattack = game.ReplicatedStorage.EnergyAttack:Clone()
--
function cubicBezier(t, p0, p1, p2, p3)
	return (1 - t)^3*p0 + 3*(1 - t)^2*t*p1 + 3*(1 - t)*t^2*p2 + t^3*p3
end
--
local uips = game:GetService("UserInputService")
--
uips.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		print("q pressed")
		local torso = character.Torso
		energyattack.CFrame = torso.CFrame + torso.CFrame.LookVector * -5
		energyattack.Parent = workspace
		print()
		for i = 0,1,0.1 do
			print(i)
			cubicBezier(i,0,0,0,Vector3.new(10,10,10))
		end
	end
end)
1 Like

you are adding a vector3 to to numbers example “13+Vector3.new(10,10,10)” which just does not work

1 Like

the api shows that, what do i use instead of it

1 Like

I don’t know I’m not the best at math but all I know is that you can’t have a number then add a vector3 (5+Vector3) you can only add numbers to numbers and vector3 to vector3.
Using Vector3.new().Magnitude will return the length of the vector (a number) so maybe you could use thay but might break the equation

1 Like

ps must be vectors. Adding some code works fine.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local energyattack = game.ReplicatedStorage.EnergyAttack:Clone()
--
function cubicBezier(t, p0, p1, p2, p3)
    return (1 - t)^3*p0 + 3*(1 - t)^2*t*p1 + 3*(1 - t)*t^2*p2 + t^3*p3
end
--
local uips = game:GetService("UserInputService")
--
uips.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        print("q pressed")
        local origin = character.Torso.CFrame + character.Torso.CFrame.LookVector * -5 :: CFrame
        energyattack.CFrame = origin
        energyattack.Parent = workspace
        print()
        for i = 0,1,0.05 do
            task.wait()
            print(i)
            local p = cubicBezier(i,Vector3.new(0,0,0),Vector3.new(0,0,0),Vector3.new(0,0,0),Vector3.new(0,10,-10))
            energyattack.Position = origin:PointToWorldSpace(p)
        end
    end
end)

what do you mean p must be vector vectors, also what does the origin do

I think what she means is you have to make all of them vectors since you’re trying to get the world position and you’d have to use vectors for that.

the origin is basically the middle of the part and can probably be replaced with cframe if you want to

sorry syntax error. i meant to say that p0, p1, p2 and p3 should be vectors.
origin is where the projectile starts (i.e. energyattack). But I only added that code to prove that it works, you can put your own code.

All the ‘p’ inputs to cubicBezier must be vectors. Instead of 0 you probably meant Vector3.zero, which is (0,0,0)

how do i make the curve smoother
https://gyazo.com/0d94cf367298cd0c96d7edbfe4fae226

I smoothed the curve but how do i stop the curve from going so far away from the player
https://gyazo.com/a445b670cc49487d3fbd4d329d7fde41

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character.Humanoid
local mouse = player:GetMouse()
local energyattack = game.ReplicatedStorage.EnergyAttack:Clone()
--
function cubicBezier(t, p0, p1, p2, p3)
	return (1 - t)^3*p0 + 3*(1 - t)^2*t*p1 + 3*(1 - t)*t^2*p2 + t^3*p3
end
--
function randomize(lastpos, target)
	local pts = lastpos - target + Vector3.new(math.random(1,10),math.random(1,10),0)
	print("randomized pts ", pts)
	return pts
end
--
local uips = game:GetService("UserInputService")
--
uips.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		print("q pressed")
		local torso = character.Torso
		energyattack.CFrame = torso.CFrame + torso.CFrame.LookVector * -5
		energyattack.Parent = workspace
		local lastpos = energyattack.Position
		local target = mouse.Hit.Position
		print()
		for i = 0,1,0.1 do
			print(i,energyattack.Position, "target position ".. i)
			local p = cubicBezier(i,lastpos,randomize(lastpos, target),randomize(lastpos, target),target)
			energyattack.Position = p
			game:GetService("RunService").Heartbeat:Wait()
		end
	end
end)

t is the % it is through the curve 0 being 0% and 1 being 100%. If you want to stop it when it reaches player stop when t reaches 1.