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?
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