Teleport system that moves the player in whatever direction they are facing

I want to make a system so that when the player presses the E key, they do a teleport in whatever direction they are facing, even upwards or downwards. Of course, I don’t want them to be able to go through or into parts with CanCollide enabled with this ability. I suppose it is good to think of it like the Flow ability from Bendy And The Dark Revival.
I currently have this script:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local plr = Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()

local teleportDistance = 20 -- The distance to teleport
local teleportIncrement = 1 -- The distance to increment when checking for collisions

local function teleport()
    local humanoid = character:FindFirstChild("Humanoid")
    local rootPart = character:FindFirstChild("HumanoidRootPart")
    
    if humanoid and rootPart then
        -- Calculate the teleport direction
        local direction = rootPart.CFrame.LookVector
        local targetPosition = rootPart.Position + direction * teleportDistance
        
        -- Check for collisions in the teleport path
        local currentPos = rootPart.Position
        local teleportVector = direction * teleportIncrement
        
        while (currentPos - targetPosition).Magnitude > teleportIncrement do
            currentPos = currentPos + teleportVector
            
            local raycastResult = workspace:Raycast(currentPos, direction, nil, nil, true)
            if raycastResult then
                -- Collision detected
                return
            end
        end
        
        -- No collision detected
        rootPart.CFrame = CFrame.new(targetPosition)
    end
end

UserInputService.InputBegan:Connect(function(input, isProcessed)
    if isProcessed then return end
    
    if input.KeyCode == Enum.KeyCode.E then
        teleport()
    end
end)

This does work, but only along the X axis. I want to player to be able to also look straight up and teleport along the Y axis (This is a first person movement game, so it’s likely to happen). Thanks in advance for any help that is offered.

instead of using “rootPart.CFrame = CFrame.new(targetPosition)” use “character:MoveTo(targetPosition)”

That hasn’t really changed much. It actually detached me from my character’s body. It also still doesn’t allow me to go upwards when I look up etc.

oh sorry didnt read the full thing but what i said should work in a better way


This is what happens when I try your suggested method. (By the way, that is the actual character, not a view model)

Strange… Worked for me

I’ll try to check it without a few other scripts enabled, I’ll come back and say if it makes a difference.

For some reason it didn’t like the script that I was using to set LocalTransparencyModifiers. I’ve edited it a little and now it works fine without the player detaching from the character. I still have the problem of it only going horizontal though…if it is a really confusing thing to implement then I can just scrap it and add other ways to gain height instead, but I’m not sure.

-- Calculate the teleport direction
local direction = rootPart.CFrame.LookVector
  
-- Check for collisions in the teleport path
local currentPos = rootPart.Position
local directionVector = direction * teleportIncrement

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.FilterType.Exclude
raycastParams. FilterDescendantsInstances = {character}
  
local raycastResult = workspace:Raycast(currentPos, directionVector, raycastParams)

if raycastResult then
    return
end
  
-- No collision detected
rootPart:PivotTo(rootPart.CFrame * CFrame.new(0, 0, teleportDistance))

I gave this a go, but still no luck. I’ve gotten used to the horizontal dash now though, and the movement for the game feels pretty good, so I’m going to leave it as is now.
Thanks to both of you for trying to help!

Replace the last line with this

rootPart:PivotTo(rootPart.CFrame * CFrame.new(0, 0, -teleportDistance))

If you want to teleport character in the direction they are facing instead of using rootPart.CFrame.LookVector use player camera’s LookVector (game.Workspace.CurrentCamera.CFrame.LookVector) as direction

2 Likes

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