Ad
Log in to vote
0

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?

Wait(1)
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

game:GetService("UserInputService").InputBegan:connect(function(input)
    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 = CFrame.new(Mouse.Hit.p)
            Torso.CFrame = CFrame.Angles(xRot, yRot, zRot);
            wait(0)
        else 
            print("Too far")
        end
    end
end)
0
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
0
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