Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get a specific position between two positions ?

I tried to make a script that tp you to the middle position between two others but it didnt work. I hope i'll know why soon

Here is my script :

local pos1 = game.Workspace.PTest1.Position
local pos2 = game.Workspace.PTest2.Position

local distance = (pos1 - pos2).magnitude

game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(distance / 2)

1 comment

0

Just use (pos1+pos2)/2. You can add and divide vectors.

3 answers

✓ Accepted answer

You can use a function of CFrame called lerp With it, you can not only get the middle pos between two position, but pretty much any as long as you provide it as a perecentage.

local pos1 = workspace.PTest1.CFrame
local pos2 = workspace.PTest2.CFrame

local middlePos = pos1:lerp(pos2, 0.5) --0.5 means 50% therefor the middle

game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = middlePos

3 comments

0

who the heck downvoted this, i gave an upvote and it was at +1 and now it's at 0.

0

because theres a much easier method.

1

but that's not a reason to downvote this answer. in fact you should be using this method.

Just do (pos1+pos2)/2. Its as easy as that

Roblox has a nice function called DistanceFromCharacter. I'd recommend using it.

2 comments

0

Thank you for this but i would like to get the exact middle position. Let's say there's Two caste. Between those 2 castle, there's a part and i want to tp to this part without knowing its position

0

DistanceFromCharacter is deprecated from what I've heard.

This discussion is preserved as it was. New answers, comments, and votes are closed.