How to add delay to my viewmodel

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to add a little delay to my viewmodels whenever i move like if you move forward it gets closer to you and if you go backwards it further away etc

  2. What is the issue? Include screenshots / videos if possible!
    i do not know how i would do it

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i looked for solutions but i couldnt find any

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local Modules = ReplicatedStorage.Modules
local Remotes = ReplicatedStorage.Remotes

local SharedServices = Modules.SharedServices

local GlobalFuncs = require(ReplicatedStorage.GlobalFuncs)
local Classes = require(ReplicatedStorage.Classes)

local RemoteFunction = Classes.RemoteFunction
local BridgeNet = Classes.BridgeNet2
local TableUtil = Classes.TableUtil
local Promise = Classes.Promise
local Signal = Classes.Signal
local Trove = Classes.Trove

if GlobalFuncs:GetEnviroment(1) then
	warn("You cannot use this module on the server.")
	return {}
end

local Camera = workspace.CurrentCamera

local ViewModelController = {}
ViewModelController.__index = ViewModelController

function ViewModelController.new(Shared)
	local Cleanup = Trove.new()
	local Model = Shared.Model:Clone()
	local self = {
		Player = Shared.Player,
		Model = Cleanup:Add(Model),
		Trove = Cleanup,
		RenderConstant = Shared.Constant,
		Offset = Shared.Offset or CFrame.new(0,0,0),
		Bobbing = Shared.Bobbing or {
			time = 0,
			intensity = 0.1,
			speed = 8
		},
		Destroyed = Signal.new()
	}
	self = setmetatable(self, ViewModelController)
	return self
end

function ViewModelController:Update(deltaTime)
	local Model: Model = self.Model
	local Bobbing = self.Bobbing
	
	self.Bobbing.time += deltaTime
	
	local X = math.sin(Bobbing.time * Bobbing.speed) * Bobbing.intensity
	local Y = math.abs(math.cos(Bobbing.time * Bobbing.speed)) * Bobbing.intensity
	
	Model:PivotTo(Camera.CFrame * self.Offset * CFrame.new(X, Y, 0))
end

function ViewModelController:Initialize()
	local Model: Model = self.Model
	local Player: Player = self.Player
	
	Model.Parent = Camera
	
	RunService:BindToRenderStep(
		self.RenderConstant,
		Enum.RenderPriority.Camera.Value + 1,
		function(deltaTime)
			self:Update(deltaTime)
		end
	)
end

function ViewModelController:Destroy()
	RunService:UnbindFromRenderStep(
		self.RenderConstant
	)
	self.Destroyed:Fire()
	self.Trove:Clean()
	table.clear(self)
	self = nil
end

return ViewModelController

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Try changing the update function to something like this. (lmk if it works because I couldn’t test it obviously.)

function ViewModelController:Update(deltaTime)
	local Model: Model = self.Model
	local Bobbing = self.Bobbing
	
	self.Bobbing.time += deltaTime

	local X = math.sin(Bobbing.time * Bobbing.speed) * Bobbing.intensity
	local Y = math.abs(math.cos(Bobbing.time * Bobbing.speed)) * Bobbing.intensity

	local character = self.Player.Character
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")
	local moveDir = humanoid and humanoid.MoveDirection or Vector3.zero

	local moveOffset = Vector3.new(0, 0, 0)
	if moveDir.Magnitude > 0 then
		moveOffset = moveOffset + Vector3.new(0, 0, -moveDir.Z * 0.2)
		moveOffset = moveOffset + Vector3.new(-moveDir.X * 0.2, 0, 0)
	end

	self.CurrentMoveOffset = self.CurrentMoveOffset or Vector3.zero
	self.CurrentMoveOffset = self.CurrentMoveOffset:Lerp(moveOffset, deltaTime * 5)

	local finalOffset = self.Offset * CFrame.new(X, Y, 0) * CFrame.new(self.CurrentMoveOffset)

	Model:PivotTo(Camera.CFrame * finalOffset)
end
1 Like

Thanks, i will try it out later and will give feedback if it works