I have two parts welded together. I want to make them closer together. I can get the point where I want part1 to be by doing newpoint = (part0.Position - part1.Position).magnitude - 1, but how do I turn that into a vector3 for the new weld?
If a
and b
are positions, then b - a
is called the displacement from a
to b
.
The displacement is the way to get from a
to b
. You travel by using +
. Thus,
d = b - a y = a + d -- == a + (b - a) == b
A simple way to go nearly there is to go, only, say, 90% of the way. You can just multiply the displacement d
by 0.9
to get that:
y = a + d * 0.9
Thus if a
and b
are 20 studs apart, d
would be 2 studs from b
.
If you wanted to be exactly x
studs from b
, you could solve that out.
Call m
the distance from a
to b
. Then (m - x) / m
is the scale you want. m
is just the magnitude of the displacement. Thus (d.magnitude - 1) / d.magnitude
is the scaling factor to use:
y = a + d * (d.magnitude - 1) / d.magnitude
Notice that there's d / d.magnitude
in there. That's just d.unit
-- it's d
but only 1 stud long:
y = a + d.unit * (d.magnitude - 1)
This makes sense. You travel (d.magnitude - 1)
studs in the direction of d.unit
from a
to get to nearly b
.
A shorter way to say this would be to start from b
and go 1
stud back:
y = b - d
which is the same as
y = b - (b - a).unit