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

Strange mouse offset happening?

Asked by 6 years ago
Edited 6 years ago

I am currently making a grid placement system but some strange offset is happening to the model I am trying to move inside the baseplate.

Take care, this is a moduleScript.

Placement_Handler.RoundByThree = function(Number)
    if math.floor(Number) == Number then
        if Number%3 == 0 then
            return Number
        elseif (Number - 1)%3 == 0 then
            return Number - 1
        elseif (Number + 1)%3 == 0 then
            return Number + 1
        end
    end
    return Number
end

Placement_Handler.placeModel = function(plrMouse, modelVisual, modelHitbox)
    local LastPosition = CFrame.new();
    while true do
        if plrMouse.Target then
            if plrMouse.Target.Name == "main_Base" then
                local mainBase = plrMouse.Target;
                modelVisual.Parent = mainBase.Parent;

                local mainBaseSizeX, mainBaseSizeZ = mainBase.Size.X, mainBase.Size.Z;
                local hitboxSizeX, hitboxSizeZ = modelHitbox.Size.X, modelHitbox.Size.Z;

                local mainBaseTopLeft = mainBase.CFrame * CFrame.new(Vector3.new(mainBaseSizeX/2, 0, mainBaseSizeZ/2));
                local baseAngleX, baseAngleY, baseAngleZ = CFrame.new(mainBase.Position, (mainBase.CFrame*Vector3.new(1, 0, 0))):toEulerAnglesXYZ();
                local VectorRotation = CFrame.Angles(baseAngleX, baseAngleY, baseAngleZ);

                local RoundByThreeX = Placement_Handler.RoundByThree(plrMouse.Hit.p.X - mainBaseSizeX);
                local RoundByThreeZ = Placement_Handler.RoundByThree(plrMouse.Hit.p.Z - mainBaseSizeZ);

                if RoundByThreeX < -mainBaseSizeX + hitboxSizeX then
                    RoundByThreeX = RoundByThreeX + hitboxSizeX;
                elseif RoundByThreeZ < -mainBaseSizeZ + hitboxSizeZ then
                    RoundByThreeZ = RoundByThreeZ + hitboxSizeZ;
                end

                local VectorComponent1 = CFrame.new(Vector3.new(mainBaseTopLeft.X, plrMouse.Target.Position.Y, mainBaseTopLeft.Z));
                local VectorComponent2 = CFrame.new(Vector3.new(RoundByThreeX, plrMouse.Target.Size.Y/2, RoundByThreeZ))* VectorRotation;
                local VectorComponent3 = CFrame.new(Vector3.new(-hitboxSizeX/2, modelHitbox.Size.Y/2, -hitboxSizeZ/2));

                local NewVector = VectorComponent1*VectorComponent2*VectorComponent3;

                local RegionVectorMin = NewVector * CFrame.new(Vector3.new(hitboxSizeX/2, -modelHitbox.Size.Y/2, hitboxSizeZ/2)); -- TopLeft
                local RegionVectorMax = NewVector * CFrame.new(-Vector3.new(hitboxSizeX/2, -modelHitbox.Size.Y/2, hitboxSizeZ/2)); -- BottomLeft / Counterpart

                local Hitboxes = game.Workspace:FindPartsInRegion3(
                    Region3.new(
                        Vector3.new(
                            math.min(RegionVectorMin.X, RegionVectorMax.X),
                            math.min(RegionVectorMin.Y, RegionVectorMax.Y),
                            math.min(RegionVectorMin.Z, RegionVectorMax.Z)
                        ) + Vector3.new(0.1, 0, 0.1),
                        Vector3.new(
                            math.max(RegionVectorMin.X, RegionVectorMax.X),
                            math.max(RegionVectorMin.Y, RegionVectorMax.Y),
                            math.max(RegionVectorMin.Z, RegionVectorMax.Z)
                        ) - Vector3.new(0.1, 0, 0.1)
                    ), nil, 100
                )

                local ModelColliding = false;
                for i, Hitbox in pairs(Hitboxes) do
                    if Hitbox.Name == "Hitbox" and not Hitbox:IsDescendantOf(modelVisual) then
                        ModelColliding = true;
                    end
                end

                if not ModelColliding then
                    LastPosition = NewVector;
                end
                modelVisual:SetPrimaryPartCFrame(LastPosition);
            end
        end
    wait();
    end
end

It literally does this weird offset: Click here!

Help would be appreciated!

0
try messing around with the mouse times or divide to try and make it synced it will take a bit of trial and error but you will get there eventually ForgotenR4 68 — 6y
0
I've already tried by myself messing around with it and I can't clearly find the fix to it, that's why I'm asking here for help. iDarkGames 483 — 6y
0
Im having the exact same problem here as well ;-; PenguinDevs 45 — 5y

Answer this question