How do i make an ability that teleports the player behind a player that hes looking at

I’m trying to make a move that lets you teleport behind the player your looking at but I have no idea what I should add or do to make that happen
I was able to script the teleportation but I’m not too sure what to do for the player selection, what the script does is just teleport to the nearest thing that has a humanoidRootPart instead of teleporting to the player that it’s looking at
https://gyazo.com/6aafeab2a70e258bdcde3e432e2526cb

I’m trying to make the teleport work similar to aba’s Afterimage move
https://gyazo.com/a408ff21b8c7c487807436f36f98a85c

1 Like

To get the player the player is looking at, you can use Raycasting. This should work,

local result = workspace:Raycast(hrp.Position,hrp.CFrame.LookVector * 1000)

if result and game.Players:GetPlayerFromCharacter(result.Instance.Parent) then
    local otherhrp:BasePart = result.Instance.Parent.HumanoidRootPart

    local LV = otherhrp.CFrame.LookVector
    hrp.CFrame = CFrame.lookAt(-LV * 5,otherhrp.Position)
end
1 Like

get the CFrame.LookVector of the camera (where the camera is looking)
then loop through each player, and get a CFrame.LookVector of the camera looking towards each player

** This is pretty much pseudo code, just to display how the loops would look **

local previousDist = math.huge
local previousTarget = nil
local camVector = camera.CFrame.LookVector
for _,player in pairs(game.Player:GetPlayers()) do
  local lookVector = CFrame.lookat(camera.Position,player.Character.PrimaryPart.Position).LookVector
  <get angle size between vectors  camVector and lookVector>
  if angle difference < previousDist then 
    previousDist = difference
    previousTarget = player
  end
end

Inside that loop after you get lookVector you compare the angles between where the camera is looking and where the camera would face if looking at a player. Then you find the smallest distance, and that is who the target should be.

This will find the closest player to where your camera is looking
after that you will then need to make sure that target is within a certain angle so that you know they are in visible range.

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