Help make projectile motion relative to a part

Hey, I recently made a projectile system and I’ve noticed an issue. The projectiles motion is in world space. For example if the projectiles acceleration is an vector of (10,0,0), the projectile will always accelerate towards positive X no matter where it was spawned at.

I want the projectile to accelerate relative to the starting point.

This is the script:


local CacheProjectiles = {}


function renderStepProjectiles(t,dt)
    for visual,projectile in CacheProjectiles do

        local newVelocity = projectile.velocity + (projectile.accel * dt)
        local newPosition = projectile.position + (steppedVelocity * dt)
        
        projectile.velocity = newVelocity
        projectile.position = newPosition

        visual.CFrame = CFrame.new(projectile.position, projectile.position + projectile.velocity) -- put at new position and make it look towards the direction
    end    
end


function InsertProjectileToCache(position,direction)
    local Data = require(script.Parent.Parent.Other.BallData) -- just a table containing data for the projectile
    
    local visualizingPart = Data.VisualModel:Clone()
    visualizingPart.Parent = workspace.debris
    
    local new = {
        origin = position
        position = position,
        velocity = direction * Data.Velocity,
        accel = Data.Acceleration,
        visualModel = Data.VisualModel:Clone();
    }
    
    visualizingPart.CFrame = CFrame.new(position)
    CacheProjectiles[visualizingPart] = new
    
end

function BallClient.Bind()
    RunService.Stepped:Connect(renderStepProjectiles)
    ev_ClientBall.Event:Connect(InsertProjectileToCache)
end

This is a module script, ev_ClientBall is just a bindable event that fires whenever a new projectile gets shot, the parameters position and direction being position of the handle of the tool and direction from tool handle to mouse position.

Here’s the issue on video btw:
https://gyazo.com/95ff7df9eeb57ce57fbd15e9c7bb5b5a
(the acceleration here is (-50,0,0) )

I guess, the problem where the ball always accelerated in a fixed world space direction You should Calculate Initial Local Orientation like

local initialCFrame = CFrame.lookAt(position, position + direction)
local localAccel = initialCFrame:vectorToWorldSpace(Data.Acceleration)

Soo the CFrame.lookAt(position, position + direction) create Cframe that aligns with the direction the ball is facing initially also the ball now accelerates relative to its starting direction, ensuring dynamic and context-aware motion.

1 Like

great, it worked! thanks for the solution.