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

Smoothing for placement system?

Asked by
zblox164 531 Moderation Voter
5 years ago

I have a working placement system but I don't like that it instantly snaps to the grid without moving to the position slowly. Should I use lerping, tweening or neither? I have tried to use both but they did not work. Here is my current code without the smoothing (so you can see what I have):

-- Defines variables
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera

local PosX
local PosY
local PosZ
local YPos = Vector3.new(-1, 75, 0)
local Rot = 0

local Model
local Button = script.Parent.TextButton
local Button2 = script.Parent.PartB
local GridSize = 2

local CanStart = true
local IsPlacing = nil
local CanPlace = nil

-- Snaps the model to grid
local function Snap()
    PosX = math.floor(Mouse.Hit.X / GridSize + 0.5) * GridSize -- Simple math
    PosY = 2
    PosZ = math.floor(Mouse.Hit.Z / GridSize + 0.5) * GridSize
end

-- Places the model
local function Placement()
    if IsPlacing and CanPlace then
        local PlacedModel = Model:Clone()
        local NOCanCollide = PlacedModel:GetChildren()
        PlacedModel.Parent = workspace.PlacedObjects

        for i, v in pairs(NOCanCollide) do
            v.CanCollide = true
        end

        IsPlacing  = false
        CanPlace = false
        CanStart = true

        Model:Destroy()
    end
end

-- Top down view
local function TopDownView()
    if IsPlacing then
        local PlayerPos = Char:FindFirstChild("HumanoidRootPart").Position
        local CameraPos =  PlayerPos + YPos

        Camera.CFrame = CFrame.new(CameraPos, PlayerPos)
    end
end

-- Moves the model
local function Movement()
    if IsPlacing and CanPlace and not CanStart then
        Mouse.TargetFilter = Model

        Snap()

        Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ) * CFrame.Angles(0, math.rad(Rot), 0))
    else
        return Mouse.Hit.p
    end
end

-- Starts the placement for a crate
local function StartPlacementCrate()
    if CanStart then
        Model = game:GetService("ReplicatedStorage").Crate:Clone()
        Model.Parent = workspace
        IsPlacing = true
        CanPlace = true
        CanStart = false        

        Mouse.Move:Connect(Movement)
    end
end

-- Starts placement for a part
local function StartPlacementPart()
    if CanStart then
        Model = game:GetService("ReplicatedStorage").Part:Clone()
        Model.Parent = workspace
        IsPlacing = true
        CanPlace = true
        CanStart = false        

        Mouse.Move:Connect(Movement)
    end
end

-- Rotation function
local function Rotation(input, GPE)
    if IsPlacing and CanPlace and not CanStart and input.KeyCode == Enum.KeyCode.R then
        Rot = Rot + 90

        Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(Rot), 0))
    end
end

-- Calls
Mouse.Button1Down:Connect(Placement)

Button.MouseButton1Click:Connect(StartPlacementCrate)
Button2.MouseButton1Click:Connect(StartPlacementPart)

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, TopDownView)
UIS.InputBegan:Connect(Rotation)

I have tried this for smoothing (Tween):

-- Only am going to show the movement function since that is where I edited it

local function Movement()
    if IsPlacing and CanPlace and not CanStart then
        Snap()

        local info = TweenInfo.new(
            5,
            Enum.EasingStyle.Linear,
            Enum.EasingDirection.Out,
            1,
            false,
            0
        )

        local Tween = TweenService:Create(Model, info, {cframe = Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ)) * CFrame.Angles(0, math.rad(Rot), 0)})

        Tween:Play()
    else
        return Mouse.Hit.p
    end
end

--FAIL

And this (Lerp):

local function Movement()
    if IsPlacing and CanPlace and not CanStart then
        Mouse.TargetFilter = Model

        Snap()

        local Cframe1 = Model.PrimaryPart.CFrame:Lerp(PosX, PosY, PosZ, 0.5)

        Model:SetPrimaryPartCFrame(Cframe1)
    else
        return Mouse.Hit.p
    end
end

Again it failed. I know I am probably doing this completely wrong because both of them gave errors. At least the tweening version still moved the model just not the way I want. Thanks for any help because I am completely lost.

Answer this question