Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

rotation resets when the mouse moves?

Asked by 4 years ago

localscript that rotates “ghostmodel”:

local player = game.Players.LocalPlayer
local char = player.Character
local space = game.Workspace
local isrotating = false




local ContextActionService = game:GetService("ContextActionService")
function onKeyPress(actionName, userInputState, inputObject)
     if userInputState == Enum.UserInputState.Begin then
       print("began")--begin block of code when button is presses--
isrotating = true

    elseif userInputState == Enum.UserInputState.End then
        print("ended")--block of code for end--
isrotating = false
    end
end

ContextActionService:BindAction("keyPressRotate", onKeyPress, false, Enum.KeyCode.R)



while true do -- block of code above controls code below--
    wait()
    local ghostmodel = script.Parent:FindFirstChild("WeakBlock")
if ghostmodel and isrotating == true then
    local cframe = ghostmodel.PrimaryPart.CFrame -- Current CFrame
    ghostmodel:SetPrimaryPartCFrame(cframe * CFrame.Angles(0, math.rad(0.5), 0))
    end
end

localscript that moves “ghostcopy” to the mouse:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

ghostcopy = game.ReplicatedFirst.ClientModels.WeakBlock:Clone()
local isequipped = false
script.Parent.Equipped:Connect(function()
    isequipped = true
    ghostcopy.Parent = script.Parent
end)
script.Parent.Unequipped:Connect(function()
    isequipped = false
end)




while true do
    wait()
    if isequipped == true then
        mouse.Move:Connect(function()

mouse.TargetFilter = ghostcopy
local cframe = ghostcopy.PrimaryPart.CFrame
local newrot = ghostcopy.PrimaryPart.Orientation.Y

local position = Vector2.new(mouse.X, mouse.Y)

local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)

local normalizedposition = position / size

ghostcopy.PrimaryPart.CFrame = CFrame.new(mouse.Hit.p)


        end)
        end
end

the model moves around and rotates when R is pressed as it should be. but after rotating the model with R, moving the mouse around resets the rotation of the model back to before it was rotated. I suspect I need to add something to {ghostcopy.PrimaryPart.CFrame = CFrame.new(mouse.Hit.p) } but im not sure what I need to add.

1 answer

Log in to vote
1
Answered by
Prestory 1395 Moderation Voter
4 years ago

You need to * the parts CFrame by the rotation you can get the rotation by subtracting the parts CFrame by its position like i have done below

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

ghostcopy = game.ReplicatedFirst.ClientModels.WeakBlock:Clone()
local isequipped = false
script.Parent.Equipped:Connect(function()
    isequipped = true
    ghostcopy.Parent = script.Parent
end)
script.Parent.Unequipped:Connect(function()
    isequipped = false
end)




while true do
    wait()
    if isequipped == true then
        mouse.Move:Connect(function()

mouse.TargetFilter = ghostcopy
local cframe = ghostcopy.PrimaryPart.CFrame
local newrot = ghostcopy.PrimaryPart.Orientation.Y

local position = Vector2.new(mouse.X, mouse.Y)

local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)

local normalizedposition = position / size

local Rotation = ghostcopy.PrimaryPart.CFrame - ghostcopy.PrimaryPart.Position
ghostcopy.PrimaryPart.CFrame = CFrame.new(mouse.Hit.p) * Rotation


        end)
        end
end
Ad

Answer this question