How to make moving spin part?

how to make a part that moves in one direction and spinning at the same time

i tried to make it but when i adding a spinning script part starts moving weirdly, not in one direction

1 Like

Add this script to the part

-- Add this script to the part

local part = script.Parent

-- Movement variables
local direction = Vector3.new(1, 0, 0) -- Direction of movement (e.g., along the X-axis)
local speed = 5 -- Speed of movement

-- Spinning variables
local rotationSpeed = 90 -- Degrees per second

-- Function to update the part's position and rotation
local function updatePart(deltaTime)
	-- Move the part
	part.Position = part.Position + direction * speed * deltaTime
	
	-- Rotate the part
	local rotation = CFrame.Angles(0, math.rad(rotationSpeed * deltaTime), 0)
	part.CFrame = part.CFrame * rotation
end

-- Main loop
while true do
	local deltaTime = game:GetService("RunService").Heartbeat:Wait()
	updatePart(deltaTime)
end
2 Likes

To explain the code above:

Roblox’s physics takes both the part’s position and rotation into account when the physics calculations are done, resulting in the janky movement you encountered.
I’m sure that there’s some object you can use to make it possible to rotate a part while it’s being flung but I’m unaware of such a thing.

But the way to do this in code would be to simply do the physics calculations yourself by launching the part using it’s Position property as described in the code posted by the poster above me to actually move the part towards to where it’s being launched.
And then using the .CFrame property to rotate the part while it’s being launched.

Example from my own projectile module:

function Behavior.LengthChanged(userData, lastPos, curPos)
		userData.Projectile.Position = lastPos + curPos
		userData.Projectile.CFrame = userData.Projectile.CFrame * CFrame.Angles(math.rad(5), math.rad(5), math.rad(5))
	end

The code above is just setting the position of the projectile to the position it’s supposed to be at and then alters the CFrame to alter it’s rotation.

This will give you the desired effect.

Result seen when the projectile impacts in this video

1 Like