So I’m working on something that uses a part with linear velocity attached to it to move this part to another part right in front of it.
This is an example of what i’m doing
However lets say I want the part to continue moving towards the other part but instead slightly offset to the other part. Like what I have drawn below
So the idea here is that when a button is pressed the part quickly corrects its path to be slightly offset whilst continuing to move towards the goal part.
For this it’d be better if you use AlignPosition. Keep the LinearVelocity so that the part keeps moving, and use the AlignPosition to change the X (or Z) axis.
-- Create parts in Roblox Studio and name them "MovingPart" and "TargetPart"
-- Attach a LinearVelocity constraint to "MovingPart"
-- Example script to attach LinearVelocity (put this in a Script in ServerScriptService)
local movingPart = workspace:WaitForChild("MovingPart")
local targetPart = workspace:WaitForChild("TargetPart")
local linearVelocity = Instance.new("LinearVelocity", movingPart)
linearVelocity.Attachment0 = movingPart:FindFirstChildOfClass("Attachment") or Instance.new("Attachment", movingPart)
linearVelocity.VectorVelocity = Vector3.new(0, 0, 0) -- Initial velocity
linearVelocity.MaxForce = math.huge
Step 2: Script to Adjust Path with Offset
script to adjust the path dynamically based on user input (e.g., a button press).
local userInputService = game:GetService("UserInputService")
local offset = Vector3.new(5, 0, 5) -- Example offset (adjust as needed)
local speed = 50 -- Speed of the moving part
local function moveToTargetWithOffset()
-- Calculate the direction towards the target part
local direction = (targetPart.Position - movingPart.Position).Unit
-- Apply the offset to the direction
local adjustedDirection = direction + offset.Unit
-- Set the velocity of the LinearVelocity constraint
linearVelocity.VectorVelocity = adjustedDirection * speed
end
-- Bind the function to a button press (e.g., "E" key)
userInputService.InputBegan:Connect(function(input, processed)
if not processed and input.KeyCode == Enum.KeyCode.E then
moveToTargetWithOffset()
end
end)