Scripting - Movement for good scripter

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.

All questions and things you want me to write are below. :grin:

4 Likes

Overall not a shabby topic, nice job. But I think it wouldn’t help a rookie reader as much as it would confuse him. Certain mentioned bits and parts although interesting are unnecessary. I doubt mentioning CFrame methods such as :ToEulerAnglesXYZ() and others is helpful. Just show a first example where the the values for CFrame.Angles() are given manually (I mean raw numbers). You seem to have skipped the absolute basics very quickly.

As well :ToAxisAngle() returns two things: an axis, which is a vector, and a number which is the rotation along that axis in radians. Using the X and Y and Z of the axis to construct a rotation CFrame doesn’t make much sense, those designate a position.

Additionally, adding visuals such as pictures would be nice, and I think stopping to explain what velocity is and visualize it is a good idea too.

5 Likes

Thanks for your contribution, I’m now targeting people with programming experience who are confused about where to start.
I will definitely dedicate a section to tutorials for newbies.
I had a bit of negligence while writing :ToAxisAngle()

2 Likes