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
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
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.