Log in to vote

How can i keep orientation of Player after cframe teleport to mouse hit?

Asked by
J_Deku 0
2 days ago

In my code i have a mouse hit teleport that saves the CFrames rotation in Euler angles before the teleport then restores it using CFrame.Angles after

However instead of having the desired effect of rotating the player correctly where it was before, it moves the player back to the center of the world, sometimes pushing it through the floor

Can someone please go over my code and tell me what I'm using wrong?

local Player = game.Players.LocalPlayer
local Cam = workspace.CurrentCamera
local Char = Player.Character
local Torso = Char:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()

local TeleportRange = 450

    if input.KeyCode == Enum.KeyCode.Z then
        local Distance = (Mouse.Hit.p-Torso.Position).Magnitude
        if Distance < TeleportRange then
            local xRot, yRot, zRot = Torso.CFrame:ToEulerAnglesXYZ();
            Torso.CFrame =
            Torso.CFrame = CFrame.Angles(xRot, yRot, zRot);
            print("Too far")
On line 16, you are resetting the positional value of the CFrame, you should instead do Torso.CFrame *= CFrame.Angles(...). This will rotate the current CFrame with respect to it's current position without altering it. Note: *= is the same as writing Torso.CFrame = Torso.CFrame * CFrame.Angles(...) climethestair 1647 — 2d
Omg you life saver, been at this for hours! Thanks so much. You should have answered this instead so i could set it as the Post answer! Much love J_Deku 0 — 2d

Answer this question