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.