You want to move things, move them, or something like that. This article will guide you on ways to do that.
Define
Movement is the change of position.
Knowledge
Definitely read all of these basics.
Vector3, BasePart, CFrame, TweenService, Model, Constraints, BodyMovers, RunService.
Scripting
A part(BasePart)
1.Direct reference
1.1.Position:This is the most basic property representing the position of BasePart.
part.Position= Vector3.new(0,0,0) --move to position 0,0,0
1.2.CFrame:Stronger property to indicate position and angle.
part.CFrame= CFrame.new(Vector3.new(0,0,0))-- move to position 0,0,0
With the above code, the rotation angle of BasePart will be default (may be different from before).
To fix this, you need to add its angle.
local part= script.Parent
local axis,numer= part.CFrame:ToAxisAngle()
part.CFrame= CFrame.new(Vector3.new(0,0,0))*CFrame.fromAxisAngle(axis,numer) -- move to position 0,0,0
or
local x,y,z=part.CFrame:ToEulerAnglesXYZ()
-- or local y,x,z=part.CFrame:ToEulerAnglesYXZ()
part.CFrame= CFrame.new(Vector3.new(0,0,0))*CFrame.Angles(x,y,z)
1.3.Anchored + Velocity
This is an alternative before the ApplyImpulse function works.
-- part moves with Velocity 0,10,0
part.Anchored= true
part.Velocity= Vector3.new(0,10,0)
part.Anchored= false
2.Use other classes: BodyMovers
Add it in Studio or by Script.
Ex: BodyForce.
local body= Instance.new("BodyForce",part)
At this point, physics will induce the movement. But the rotate angles will not be guaranteed to be the same.
Note: BasePart is not Anchored.
Parts
Do you think you will work with each part? It would be a waste if they all move the same way.
So using constraints to bind them is a top choice.
Let’s gather them into a Model and set PrimaryPart, write code will be easier.
The application for a part will still be correct.
Note: the Position reference will break the constraints so only the main section will move.
More smoother.
This is only needed when using Position or CFrame references.
1.Use loop like while, for, repeat with wait()
is good choice.
2.In addition, RunService has provided you events with distances of about 0.02s.
3.Tween: Offers more unique moves.Only for Position and CFrame.
A small example: move the part to position 0,0,0 in 3s
local tweenService= game.TweenService
local info= TweenInfo.new(3)
local part= script.Parent
local move= tweenService:Create(part,info,{Position= Vector3.new(0,0,0)})
move:Play()
You can also leave {Position=Vector3.new(0,0,0)}
above if you want clarity.
Note: CFrame cannot use {CFrame=CFrame.new(Vector3.new(0,0,0))}
.
Thanks for reading. This is the first tutorial in a series of lessons I will be writing in the near future. I will put more effort into creating crisp and concise tutorials.