Keeping a character's rotation the same whilst changing the position with cframe

[i purposely used the old raycasts please dont recommend the new ones as it is irrelevant to the question, happens all the time thx]

hey i got this problem bugging me lately, i cast rays like these all the time but i just cant get how to keep the rootpart’s rotation unchanged, i fix it most of the times but on accident i never understood it so if anyone can explain a bit, question - in the commented line how can i keep the rootpart’s rotation unchanged like change only the position, i cant use rootpart.Position because half the time it dont replicate properly thx

    local oldPos = rootpart.Position
    local direction = rootpart.CFrame.LookVector
    local range = 50 

    local oldcf = rootpart.CFrame * CFrame.new(0,-2.9,0)
    local newPos = nil

    local ray = Ray.new(oldPos, direction * range)
    local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray, {char,workspace.Effects,workspace.Characters,workspace.CharacterHitboxes})

    if hit then
        rootpart.CFrame = CFrame.new(pos) --this line here
        newPos = rootpart.Position
    else 
        rootpart.CFrame *= CFrame.new(0,0,-45)
        newPos = rootpart.Position
    end
root part.CFrame = root part.CFrame.Rotation + pos
1 Like

well that’s pretty embarassing on my side, learned, HUGE thanks.

No prob, check out my CFrame tutorial for more tips:

1 Like