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

How do I make SetPrimaryPartCFrame smooth?

Asked by 5 years ago

I have a line in my code which basically makes the character face the mouse.

Character:SetPrimaryPartCFrame(CFrame.new(rootPart.Position, (mouse.Hit.p*Vector3.new(1,0,1)) + (rootPart.Position*Vector3.new(0, 1, 0))))

The problem is, when I enable this, the character snap-faces to the mouse, and it doesn't look appealing at all. What's the best way to make it so that it's not-so-snappy, but rather it eases the SetPrimaryPartCFrame to the mouse pointer?

I've seen people do similar things, where the CFrame initially moves fast to the target, but as it reaches the target, it slows down. Could someone nudge me in the right direction?

0
Use RenderStepped, I think that should smoothen things out. Ziffixture 6913 — 5y
0
I am using RenderStepped. The problem here is that all it's doing is SetPrimaryPartCFrame, which means it just snaps to the mouse pointer whenever I enable it. bloberous 66 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

An efficient and amazing method for smoothly interpolating properties is the TweenService.

TweenService supports interpolation for several different data types, including CFrame. You can access the service using:

game:GetService("TweenService");

Below I have outlined how you would use the TweenService to create the desired effect:

local TweenService = game:GetService("TweenService");

local TweenDetails = TweenInfo.new(
    1, --Time it takes to complete tween
    Enum.EasingStyle.Quint, --"Style" of the tween
    Enum.EasingDirection.Out, --Direction of the tween
    0, -- Repeat count
    false, -- Reverses after completion
    0 -- Delay in starting tween
);

local TweenProperties = {
        ["CFrame"] = CFrame.new(rootPart.Position, (mouse.Hit.p*Vector3.new(1,0,1)) + (rootPart.Position*Vector3.new(0, 1, 0)))
}; -- Properties we want to tween

local Tween = TweenService:Create(rootPart, TweenDetails, TweenProperties); -- Initiate tween
Tween:Play(); -- Play Tween

Because the HumanoidRootPart is welded to all the other parts in a character, you can simply tween it instead of having to tween each individual part or trying to use the :SetPrimaryPartCFrame() method. You would want to put lines 12-17 inside of the function your event is calling. Let me know if you need additional help on how to implement this.

The TweenService is a super valuable service that can save you loads of time.

Extra Reading: Types of EasingStyles

0
Hey it was you who answered another one of my questions before, thanks! I didn't know this existed. Now I wonder what else I've missed that could help immensely with my scripting. I'll let you know how it goes! bloberous 66 — 5y
0
You ask good questions! Glad to help :) GrandpaScriptin 148 — 5y
Ad

Answer this question