#lookAt and PivotTo not actually facing player
1 messages · Page 1 of 1 (latest)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local pHead = character:WaitForChild("Head")
----------- MAIN LOOP -------------
game:GetService("RunService").RenderStepped:Connect(function()
local targetPos = pHead.Position
-- SHADOW FREDDY --
local shadowFreddy = workspace.Map.Map:WaitForChild("shadowFreddyPAS")
local sFHead = shadowFreddy.up:WaitForChild("head")
local sFHeadPivot = sFHead:GetPivot()
local sFHeadPos = sFHeadPivot.Position
local sFFollow = CFrame.lookAt(sFHeadPos, targetPos, Vector3.new(0,1,0))
sFHead:PivotTo(sFFollow)
end)
From a quick read the code itself looks fine, do you have anything else that modifies any part of freddy at any point?
Well, in the video you can see him appear, but im not sure if it would affect anything like that, as its only changing the transparency of him, he is still there an in action even when not visible.
-- Managing the fading in and out --
local shadowFreddy = workspace.Map.Map.shadowFreddyPAS
local SFPrimary = shadowFreddy:WaitForChild("broworkpls")
local tweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
---- LOOP :P -----------
game:GetService("RunService").RenderStepped:Connect(function()
local character = player.Character
local hrp = character:WaitForChild("HumanoidRootPart")
local dist = (SFPrimary.Position - hrp.Position).Magnitude
if dist > 25 then
for _, parts in ipairs(shadowFreddy:GetDescendants()) do
if parts:IsA("BasePart") then
tweenService:Create(parts, TweenInfo.new(.5), {Transparency = 1}):Play()
end
end
else
for _, parts in ipairs(shadowFreddy:GetDescendants()) do
if parts:IsA("BasePart") then
tweenService:Create(parts, TweenInfo.new(.5), {Transparency = 0}):Play()
end
end
end
end)
This is the script that manages that
Hmm yeah, that code doesn't seem to be causing it.
Unrelated but probably undesired, that code is tweening every part every single frame instead of just when you cross the 25 stud distance boundary
oh?
In the video, when the head rotates wrong, can you send me a screenshot of a fully expanded explorer view of freddy's head?
Just like, his head model in the explorer?
Step through the code in your head — every frame its checking if your greater than or less than 25 studs away from freddy, and then playing a tween
Yes
I believe this is what you meant? Pardon my extremely messy workspace
Try setting the PrimaryPart of the head
To what? Just anything in the head?
Something that is aligned with the way the head is facing
Is that not what I want? I figured if it's not in the loop it will only check once, and then wont work again
That is correct, you do want to check every frame, but you only want to play a tween if the value has changed since the last time you checked it
So should I use something like a boolvalue?
Ah! It worked! I didn't even think about that. Thanks!
That might work
-# Assuming you mean use a BoolValue and listen to its Changed event. I think it would work but i'm not 100% sure if the change event would actually fire even if you assign the value that the BoolValue already contains.
It would be more efficient to remember the last value in a variable and compare it each time you check the distance
Something like this
local was_close
game:GetService("RunService").RenderStepped:Connect(function()
local dist = -- Do calculation
local is_close = dist > 25
-- If the character hasn't changed from being close or far
-- since the last frame, don't do any tweens
if is_close == was_close then return end
if is_close then
for _, parts in ipairs(shadowFreddy:GetDescendants()) do
if parts:IsA("BasePart") then
tweenService:Create(parts, TweenInfo.new(.5), {Transparency = 1}):Play()
end
end
else
for _, parts in ipairs(shadowFreddy:GetDescendants()) do
if parts:IsA("BasePart") then
tweenService:Create(parts, TweenInfo.new(.5), {Transparency = 0}):Play()
end
end
end
end)
And just for completness, here is the same code but cleaned up a bit
local tween_info = TweenInfo.new(.5)
local was_close
game:GetService("RunService").RenderStepped:Connect(function()
local dist = -- Do calculation
local is_close = dist > 25
-- If the character hasn't changed from being close or far
-- since the last frame, don't do any tweens
if is_close == was_close then return end
local transparency = if is_close then 1 else 0
for _, part in shadowFreddy:GetDescendants() do
if part:IsA("BasePart") then
tweenService:Create(part, tween_info, {Transparency = transparency}):Play()
end
end
end)