Pavel
Pavel

Reputation: 3

Why when I use movement and turn together, the turn does not work

why if i use it code - rotation is working

p_partnew.Position = Vector3.new (i,p_coord_y, p_coord_z)    
p_partnew.CFrame = p_partnew.CFrame*CFrame.Angles(p_angles_x,p_angles_y, p_angles_z)

if i use it code - rotation is NOT working

p_partnew.CFrame = CFrame.new (i,p_coord_y, p_coord_z)        
p_partnew.CFrame = p_partnew.CFrame*CFrame.Angles(p_angles_x,p_angles_y, p_angles_z)
        

Upvotes: 0

Views: 669

Answers (2)

Kylaaa
Kylaaa

Reputation: 7203

To understand what's going on, take a look at Understanding CFrames.

A CFrame is a 4x3 matrix with components corresponding to the Part's Position and Orientation. When you get or set a Part's Position property, it is just reading and writing to that specific section of the CFrame's values.

Let's look at some example CFrames :

Example CFrame Components
A Part located at (0, 0, 0) with no rotation
Part.CFrame = CFrame.new(0,0,0)
0 0 0 1
0 0 0 1
0 0 0 1
A Part located at (1, 2, 3) with no rotation
Part.CFrame = CFrame.new(1,2,3)
1 2 3 1
0 0 0 1
0 0 0 1
A Part located at (0, 0, 0) with (90, 0, 0) rotation
Part.CFrame = CFrame.new(0,0,0) * CFrame.Angles(math.rad(90), 0, 0)
0 0 0 1
0 0 0 A
-1 0 1 A
A Part located at (0, 0, 0) with (0, 90, 0) rotation
Part.CFrame = CFrame.new(0,0,0) * CFrame.Angles(0, math.rad(90), 0)
0 0 0 A
0 1 0 1
0 -1 0 A
A Part located at (0, 0, 0) with (0, 0, 90) rotation
Part.CFrame = CFrame.new(0,0,0) * CFrame.Angles(0, 0, math.rad(90))
0 0 0 A
-1 0 1 A
0 0 0 1
A Part located at (1, 2, 3) with (90, 90, 90) rotation
Part.CFrame = CFrame.new(1,2,3) * CFrame.Angles(math.rad(90), math.rad(90), math.rad(90))
1 2 3 1
0 A A B
-1 0 1 B
Terms Values
A -4.3711388286738e-08
B 1.9106854651647e-15

In your first code sample, you are setting the Position first. This preserves the original CFrame, and updates just the values for Position.

-- imagine that p_partnew.CFrame looks like this :
-- ? ? ? ?
-- ? ? ? ?
-- ? ? ? ?

-- set just the position values in the CFrame, keep everything else
p_partnew.Position = Vector3.new(i, p_coord_y, p_coord_z)

-- p_partnew.CFrame now looks like this :
-- i p_coord_y p_coord_z ?
-- ? ? ? ?
-- ? ? ? ?

-- apply a transformation of angles
p_partnew.CFrame = p_partnew.CFrame * CFrame.Angles(p_angles_x, p_angles_y, p_angles_z)

In the second code sample, you are setting the entire CFrame first with just the position values. This wipes out all the other data that existed in that CFrame before.

-- set the entire CFrame
p_partnew.CFrame = CFrame.new(i, p_coord_y, p_coord_z)

-- p_partnew.CFrame now looks like this :
-- i p_coord_y p_coord_z 1
-- 0 0 0 1
-- 0 0 0 1

-- apply a transformation of angles
p_partnew.CFrame = p_partnew.CFrame * CFrame.Angles(p_angles_x, p_angles_y, p_angles_z)

So if the first example works with rotation, but the second doesn't, then the answer is that the original rotation information is getting lost when you set the CFrame. You could try saving that information first, then applying it to the new position, and then applying your changes (assuming that your changes are small increments). That would look something like this :

-- store the previous orientation
local o = p_partnew.Orientation

-- create a set of changes based on new angles
local angles = CFrame.Angles(math.rad(o.X) + p_angles_x, math.rad(o.Y) + p_angles_y, math.rad(o.Z) + p_angles_z)

-- set the new CFrame
p_partnew.CFrame = CFrame.new(i, p_coord_y, p_coord_z):ToWorldSpace(angles)

Upvotes: 0

GalaxyShard
GalaxyShard

Reputation: 306

In the first example, only the position of the part is being modified and then the rotation is applied. The second example sets the whole CFrame to the position which will override the original rotation of the object, and then applies the rotation.

Simply put, #1 adds p_angles to the rotation, while #2 sets the rotation to p_angles.

Upvotes: 1

Related Questions