Part's CFrame Manipulation is buggy

I have 2 parts.
image
Imagine there will be a tiny part that connects between those 2 parts. Normally, you change the Part.Size.Y property in order to scale to make it look like the part is connecting those 2 parts. However, when I tried to do this CFrame manipulation:

part.CFrame = PartA.CFrame:Lerp(PartB.CFrame, 0.5) * CFrame.new(part.Position, PartB.Position)

It appears like this.
image
It appears the part’s Size.X direction is looking at the green part instead of Size.Y.
BUT, if I use the same code line and instead I made the part to look at PartB’s position after doing the Lerp function:

part.CFrame = PartA.CFrame:Lerp(PartB.CFrame, 0.5)
part.CFrame = CFrame.new(part.Position, PartB.Position)

It’s normal.
image
Can anyone explain this why?

3 Likes

i think this is better

part.CFrame = CFrame.lookAt(PartA.Position:Lerp(PartB.Position,0.5),partB.Position)

the CFrame.lookAt function is like CFrame.new. First argument is basically the middle of both parts,
second is the position to look at.

In your attempt here

why are you lerping cframe, you should lerp position then
the second one is right, but use CFrame.lookAt instead, cuz they said its better

difference between your two codes?. Remember, a value will update once everything in the right side is done computing stuff. That means in here

part.CFrame = PartA.CFrame:Lerp(PartB.CFrame, 0.5) * CFrame.new(part.Position, PartB.Position)
-- CFrame.new(part.Position, PartB.Position)

you attempted to take the part’s position BEFORE it was updated

There is no Lerping function for Vector3 values.

What? Could you provide me a source saying this?

prove me wrong, type this in the command bar or a script and tell me the output

print(Vector3.new(1,0,0):Lerp(Vector3.new(0,0,0),0.5))

heres the Vector3 documentation, and go to functions. The first thing is Vector3:Lerp

i have no source but from 1 year experience in coding (and camera manipulation which uses CFrame), and literally in ALL CODING LANGUAGES, thats how that works.
Im not sure what you are thinking

a = 5
b = 7
a = (b - a) * a
print(a) -- 4? no 10

you dont set a to (b - a) then after that multiply a by itself.
you set a to (b - a) * a

I believe that’s just operator precedence, not things starting from the right, if so, can you explain why this returns 3?

print(true and 3 or 5)

The brackets have high priority than multiplication, so obviously Lua will first interpret whatever that’s inside the bracket before doing with the multiplications.

He doesn’t mean things start from the right. He means that everything on the right side of an equals is calculated before it is assigned to the value at the left side of the equals. The issue though is that you’re adding rotation instead of setting rotation in your first line of code and basing it off of the rotation of PartA and PartB. The second way of doing it would work since you aren’t adding rotations, but use Vector3.Lerp for this since it will be more efficient.

1 Like

Can you explain how Lua interprets this flow of code then?

Won’t let me delete replies again smh.

I misunderstood you. Here is how your code is interpreted.
It finds the CFrame that is halfway between (and halfway rotated between) A and B, and then ADDS the rotation of the second CFrame.new there. Then it assigns it to the property.

This code once again finds the position halfway between (and rotation halfway between) the two CFrame A nd B. It then however strips the existing rotation instead of adding onto it when you do part.Position

why that returns three?

because in lua (and probably in other languages), you can add conditions inside stuff. For example

local a = math.random(1,10)
print(a > 5)

if a is more than 5 it will print true, or else print false

local a = math.random(1,10)
print(a > 5 and "more than" or "less than")

like i said, you can add conditions, now what that code does is:

  1. set a to a random number from 1 to 10
  2. print this value
    a. check if a is more than 5
    b. if true then return “more than”, or else return “less than”

Now if you understand that, you can do stuff like this

function clampNumber(n,min,max)
	return n > max and max or n < min and min or n
end

if n is more than max, then return max,
or else if n is less than min then return min,
or else return n

If that code is a bit hard to understand, then put brackets between each or

function clampNumber(n,min,max)
	return (n > max and max) or (n < min and min) or (n)
end

so basically the photo that you sent represents the order of operations, and i think i used wrong words but thanks to @JarodOfOrbiter for correcting.

Is there any way or code so I could do all of these in one single line then?

This line by Lielmaster should do the trick.