Teleporting across an "axis"

I’m trying to make the player teleport to an equivalent position across a screen when they use a tool while standing in a valid region

Some examples in the image, when they stand in the center of the green circled area on one side for example and use the tool they get teleported to the center of the green circled area on the other side.

Problem is I’m not that great at manipulating positions yet so I have no idea how the math should work, and I also don’t know how to reliably detect if the player is standing in a valid region (the big transparent white boxes on both sides) while using the tool. I’ve tried searching here but nothing useful turned up

3 Likes

bump i still can’t figure it out

1 Like

Let’s say one of those position is (x, y, z) and we want to find the position where the player has to teleport from (x, y, z). We also define X coordinate of the white line is k.
So… the player at position (x, y, z) would need to teleport to (2k - x, y, z)

You need to provide more accurate positions/ sizes of the line and the region to get more accurate answer.

The sizes, orientations and positions of the line and regions aren’t going to be fixed as I intend to use and adjust them for different stages in the game. I’m trying to create a universal formula for the teleportation. Though the line will always be 4 studs thick and cancollide true if that helps

bump still stuck, here’s my current code in the tool’s handle (the active value is set to true by another script on click, and I currently only have it working on one of the sides for testing purposes)

script.Parent.Touched:Connect(function(hit)
	if script.Parent.Parent.Handle.Active.Value == true then
		print("lol")
		if hit.Name == "Side1" and hit.Parent.Name == "AXIS" then
			script.Parent.Parent.Handle.Active.Value = false
			
			local hrp = script.Parent.Parent.Parent.HumanoidRootPart
			local axis = hit.Parent.Axis
			local otherside = hit.Parent.Side2
			
			local relative = hit.CFrame:toObjectSpace(hrp.CFrame)
			local otherc = otherside.CFrame
			local newc = otherc * relative
			

			hrp.CFrame = newc

		end
		
	end
end)

it kind of works, but only when activated upon entering a region, and only works like the example below

where I want the player to go from red to yellow, but it goes from red to blue instead

1 Like

Is it always going to teleport you on the same axis or are you planning on making it multiple axis later?
Also will a room eventually be rotated or will it always be the same orientation?

For this problem you could use workspace:GetPartsInPart() as it returns a table of parts inside that are inside said Part.
You should be able to figure out if the player is in it by looping through all parts and checking if the Part.Parent:IsA("Model") or Part.Parent:FindFirstChild("Humanoid").

I think figured out a solution, I’m just forming an explanation and I’ll reply soon.

Ok sorry for my stupidity earlier, if you want a simpler solution you can use this instead, all it does is cast 2 rays parallel to the axis and if one of them hits the axis it teleports the player that distance*2 + the size of the wall (and multiplies by -1 if the player is in the opposite side of the room)

local hrp = workspace.frivgametrol.HumanoidRootPart
local axis = workspace.Axis
local rp = RaycastParams.new()
rp.FilterDescendantsInstances = {axis}
rp.FilterType = Enum.RaycastFilterType.Include

--Change CFrame.LookVectors to RigtVectors and Size.Z to Size.X if it doesn't work
local ray1 = workspace:Raycast(hrp.Position, axis.CFrame.LookVector*200, rp)
local ray2 = workspace:Raycast(hrp.Position, axis.CFrame.LookVector*200*-1, rp)

if ray1 and ray1.Instance == axis then
	local distance = (hrp.Position-ray1.Position).Magnitude*2
	hrp.CFrame = hrp.CFrame + axis.CFrame.LookVector*(distance+axis.Size.Z)
elseif ray2 and ray2.Instance == axis then
	local distance = (hrp.Position-ray2.Position).Magnitude*2
	hrp.CFrame = hrp.CFrame + axis.CFrame.LookVector*(distance+axis.Size.Z)*-1
end

(Do note however that this is a band-aid solution but it works just fine)

DAMN thanks for the effort, sorry for the late reply I’ll give a proper response later on

1 Like

after a couple tweaks it works beautifully! huge thanks for the help :]

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.