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

Best way to make a/ optimize this "View Model" script?

Asked by 4 years ago
Edited 4 years ago

So my question is this: What is the best way to make a/ optimize my "View Model" script?

What I mean by that is, what is the most efficient(fastest, resources-lite, best) way to make a model follow the cameras position and rotation with a delay.

...Hmm, maybe that's not the best way to describe it either...

Here's some of my code: (a local script in "StarterPack")

local Tween_Service =game:GetService("TweenService")
local Render_Stepped =game:GetService("RunService").RenderStepped
local Offset =Vector3.new(0,0,-20)

local PL_Head =script.Parent.Parent.character.Head
local Camera =workspace.CurrentCamera
local V_Offset =CFrame.new(0,-1,0)
local V_Model =script:WaitForChild("E_Model"):Clone()
V_Model.Parent =Camera

local Dynamic_MousePose =Instance.new("Part")
Dynamic_MousePose.Name ="D_MPoint"
Dynamic_MousePose.Anchored =true
Dynamic_MousePose.CanCollide =false
Dynamic_MousePose.Shape =Enum.PartType.Ball
Dynamic_MousePose.Transparency =1
Dynamic_MousePose.Size =Vector3.new(0.2,0.2,0.2)
Dynamic_MousePose.Parent =Camera

local tweenInfo =TweenInfo.new(
    0.1,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)

Render_Stepped:connect(function()
    local goal ={Position =Camera.CoordinateFrame*Offset}
    goal.Position =Camera.CoordinateFrame*Offset
    local tween =Tween_Service:Create(Dynamic_MousePose,tweenInfo,goal)
    tween:Play()
    V_Model:SetPrimaryPartCFrame(CFrame.new(PL_Head.CFrame.Position,Dynamic_MousePose.CFrame.Position)*V_Offset)
end)

Anyway, what this dose is put "E_Model"(A model) in the camera, then keeps it at the head position with an offset(V_Offset). It then Tweens a part(Dynamic_MousePose) to the center of the camera and makes the View Model rotate towards it. (causing a late drifty movement effect)

This script I made is exactly what I need, it's just a bit heavy on resources and I feel there must be a much better way to achieve this.

Things to keep in mind if you mess with it:

The values of "V_Offset" and "V_Model" need to be able to change without breaking the script. (Different models will be used with different offsets)

The model cannot be "stuck to the screen". It must move around with a bit of a daily. (Yes, I am aware that Tweening like that is probably a terrible idea, but it was the only thing I could come up with, so...)

Even if you can't completely figure out how to make it work, share your ideas anyway! (I appreciate the effort and can probably use them in the final project anyway, so please, share!)

Anyway, that's all I've got. Thank you for you time and your help!

0
Could you show a gif of what you're trying to achieve? BlackOrange3343 2676 — 4y
0
The effect I want is what this script dose. I'm just looking for a better way to do it. mbramblet 80 — 4y

Answer this question