Bezier Curve not going in correct direction

Hello! So i made a cannon that the player can get into via a proximityprompt. Once the prompt is pressed, the player will be put inside the cannon & the HumanoidRootPart will be anchored, and the player will get launched to the destination part. The problem here is that i want the curve to go up, not left or right. I can’t figure out how to fix this, i’ve tried to play around with the Vector3 values but nothing’s changing. Below is the code, a video showing the problem and the model of the cannon.

 
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local Root = Character:WaitForChild("HumanoidRootPart")
    
    local function lerp(a, b, t)
        return a + (b - a) * t
    end

    local function quadraticBezier(t, p0, p1, p2)
        local l1 = lerp(p0, p1, t)
        local l2 = lerp(p1, p2, t)
        local quad = lerp(l1, l2, t)
        return quad
    end
    
    Root.Anchored = true
    Root.CFrame = self._Start.CFrame
    
    for i = 1, 100 do
        local t  = i/100
        local Updated = quadraticBezier(t, self._Start.Position, Vector3.new(0, 100, 0), self._End.Position)

        Root.Position = Updated
        task.wait()
    end

image
image

1 Like

Why is your record lagging so much ,if maybe your pc soo strong?

It’s Bandicam, it’s always like this.

Hello there.

Notice how in the Updated variable in the for loop is set to:

local Updated = quadraticBezier(t, self._Start.Position, Vector3.new(0, 100, 0), self._End.Position)

and look how you’re setting the third parameter of the quadraticBezier() invocation, the parameter of the mid-way point, to Vector3.new(0, 100, 0), which is forcing the curve to move sideways, in this case, to satisfy reaching to X = 0 and Z = 0.

Something you may try, is grabbing the starting X and Z and the ending X and Z and find their midpoint in X and Z.
Implemented in your code, it would be something like so:

local XZAxes = Vector3.new(1, 0, 1) -- in a variable to keep it handy.
local midPointPosition = Vector3.new(0, 100, 0) + (self._Start.Position * XZAxes):Lerp(self._End.Position * XZAxes, 0.5) -- in a variable to keep it readable.
local Updated = quadraticBezier(t, self._Start.Position, midPointPosition, self._End.Position)

There, I used :Lerp() to interpolate both the starting and ending position to 0.5, their midpoint.
There’s a small possibility I messed something up since I wrote that directly into here.

Hope that helps; Please, let me know if otherwise.

It doesn’t happen on my OBS studio video recorder
Also bandicam was popular in 2010 era until 2018 they became the worst and stupid big text annoying

Sorry for the inconvenience and please don’t let the curiosity kill my cat

Something to keep in mind is that the first two lines may remain out of the for loop because they’re constant, so running them over and over is not necessary.

Implementing the snippet I gave you in your full code would be something like this:

    local Character = Player.Character or Player.CharacterAdded:Wait()
    local Root = Character:WaitForChild("HumanoidRootPart")
    
    local function lerp(a, b, t)
        return a + (b - a) * t
    end

    local function quadraticBezier(t, p0, p1, p2)
        local l1 = lerp(p0, p1, t)
        local l2 = lerp(p1, p2, t)
        local quad = lerp(l1, l2, t)
        return quad
    end
    
    Root.Anchored = true
    Root.CFrame = self._Start.CFrame
    
    local XZAxes = Vector3.new(1, 0, 1) -- in a variable to keep it handy.
    local midPointPosition = Vector3.new(0, 100, 0) + (self._Start.Position * XZAxes):Lerp(self._End.Position * XZAxes, 0.5) -- in a variable to keep it readable.

    for i = 1, 100 do
        local t  = i/100
        local Updated = quadraticBezier(t, self._Start.Position, midPointPosition, self._End.Position)

        Root.Position = Updated
        task.wait()
    end

Edit: Code formatting.

1 Like

Hello, thank you so much for the help. It finally works! I’ll be sure to keep you updated if i encounter more problems on this.

1 Like

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