PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Velocity

BasePart

Vector3

The Velocity of a part describes how its /BasePart/Position is presently changing. The unit of this property is studs per second. For reference, the default Roblox character moves at 16 studs per second via /Humanoid/WalkSpeed. The acceleration due to gravity is found in /Workspace/Gravity (by default, -196.2 studs per second).

Setting the Velocity of an part that is /BasePart/Anchored will cause it to act like a conveyor belt. Any object that touches the part will begin to move in accordance with the Velocity.

Some /BodyMover objects will apply forces and thus change the Velocity of a part over time. The simplest of these is a /BodyForce which can be used to counteract the acceleration due to gravity on a single part (set the +Y axis of the /BodyForce/Force to the product of the mass (/BasePart/GetMass) and the gravity constant).


Code Samples


Projectile Firing

This code sample fires a part from one position toward another. It calculates the velocity needed to reach the destination in time, and applies an anti-gravity effect using a BodyForce. In addition, it adds a Trail to better visualize the path of the projectile as it arcs through the air.

-- Put this Script in a Part, preferably bullet-shaped :)
local part = script.Parent
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(2, 2, 2)
part.BrickColor = BrickColor.new("Really black")
part.CanCollide = false

-- Settings for the projectile's path!
local startPoint = Vector3.new(0, 50, 0)
local targetPoint = Vector3.new(50, 100, 0)
local travelTime = 1
local antiGravity = .5

-- Anti-gravity effect: add a BodyForce to counter gravity
local bf = Instance.new("BodyForce")
bf.Force = Vector3.new(0, workspace.Gravity * part:GetMass() * antiGravity, 0)
bf.Parent = part

-- Add a trail to two attachments
local a0 = Instance.new("Attachment", part)
a0.Position = Vector3.new(1, 0, 0)
local a1 = Instance.new("Attachment", part)
a1.Position = Vector3.new(-1, 0, 0)
local trail = Instance.new("Trail", part)
trail.Attachment0 = a0
trail.Attachment1 = a1
trail.FaceCamera = true
trail.Transparency = NumberSequence.new({
	NumberSequenceKeypoint.new(0, 0),
	NumberSequenceKeypoint.new(1, 1)
})
trail.Lifetime = .35

local function fire(startPoint, targetPoint)	
	-- Calculate how far we have to travel
	local distance = (targetPoint - startPoint).magnitude
	-- Since speed = displacement / time, our speed is:
	local speed = distance / travelTime
	-- Position our part at the start, pointing to the target
	part.CFrame = CFrame.new(startPoint, targetPoint)
	-- Shoot the part
	part.Velocity = part.CFrame.lookVector * speed
end

-- Repeatedly fire the projectile
while true do
	fire(startPoint, targetPoint)
	wait(travelTime)
end