To start off, get the difference between current position and target position.
Then split that up into the length and direction, like this:
local dif = targetPosition - currentPosition
local length = dif.Magnitude
local direction = dif.Unit
Then we'll create the dif vector again, but now with a length of 25:
local dif = targetPosition - currentPosition
local length = dif.Magnitude
local direction = dif.Unit
local newDif = direction * 25
Add this to the current position, and you'll teleport 25 studs closer each time this piece of code gets executed
local dif = targetPosition - currentPosition
local length = dif.Magnitude
local direction = dif.Unit
local newDif = direction * 25
currentPosition += newDif
You don't want to overshoot with teleporting, so you need to make sure that the distance that you travel is not more than the distance between you and the target position.
You can achieve this by replacing direction * 25 with direction * math.min(25, length)
local dif = targetPosition - currentPosition
local length = dif.Magnitude
local direction = dif.Unit
local newDif = direction * math.min(25, length)
currentPosition += newDif
Now you want to teleport until the player is inside the block, so create a variable that keeps track when the player gets inside the part.
Then, you'll want to repeat the code until that variable is true.
local targetReached = false
repeat
local dif = targetPosition - currentPosition
local length = dif.Magnitude
local direction = dif.Unit
local newDif = direction * math.min(25, length)
currentPosition += newDif
until targetReached
With this done, you now want to check when the player enters the brick. You could possibly use the physics engine for this, but I'll keep it a bit simpler.
If the current distance between the player and the brick is less than or equal to 25 studs, the teleport is guaranteed to teleport the player inside the brick. Checking for this, you can set targetReached to true, and exit the loop once that happens.
local targetReached = false
repeat
local dif = targetPosition - currentPosition
local length = dif.Magnitude
local direction = dif.Unit
local newDif = direction * math.min(25, length)
currentPosition += newDif
if length <= 25 then
targetReached = true
end
until targetReached
With this all done, you only need to add a wait(3) inside the loop so you don't get teleported instantly.
local targetReached = false
local targetPosition = ...
repeat
wait(3)
local dif = targetPosition - currentPosition
local length = dif.Magnitude
local direction = dif.Unit
local newDif = direction * math.min(25, length)
currentPosition += newDif
if length <= 25 then
targetReached = true
end
until targetReached
You'll need to add a bit of code yourself, which I have added in comments,
Additionally, you might need to correct syntax errors, because I have not tested this myself.
1 comment
Perhaps you should look into .Magitude, thats a way to find the distance (in studs) between two positions, although im not sure how you could apply it to what you doing.