You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want COBALT (the shopkeeper) to look at the nearest character to him.
What is the issue? Include screenshots / videos if possible!
Instead of looking at the character correctly, COBALT’s head will rotate on one wrong axis, but rotate correctly on others.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve dug around on YouTube and the DevForum, but to no avail.
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 neck = char.Torso.UpperTorso.Chest.NeckJoint
local cframe0 = neck.C0
game["Run Service"].Heartbeat:Connect(function()
local target = findTarget(root.Position).Parent
if target then
local is_in_front = char.PrimaryPart.CFrame:ToObjectSpace(target.PrimaryPart.CFrame).Z < 0
if is_in_front then
local unit = -(char.PrimaryPart.CFrame.p - target.PrimaryPart.Position).unit
neck.C0 = cframe0 * CFrame.new(Vector3.new(0, 0, 0), unit) * CFrame.Angles(0, -math.rad(char.PrimaryPart.Orientation.Y), 0)
end
end
end)
If you want to work with the rig to better understand the issue, here it is: cobalt.rbxm (736.2 KB)
For some extra details, here are the properties of the neck motor:
i’ve figured it out! i just had to edit some values and change a few things!
game:GetService("RunService").Heartbeat:Connect(function()
local TrsoLV = char.Torso.UpperTorso.Chest.CFrame.lookVector
local headPos = head.CFrame.p
local target = findTarget(root.Position)
if target and script.HeadLook.Value == true then
local is_in_front = root.CFrame:ToObjectSpace(target.Head.CFrame).Z < 0
if is_in_front then
local dist = (head.CFrame.p-target.Head.CFrame.p).magnitude
local diff = head.CFrame.Y-target.Head.CFrame.Y
local unit = -(root.CFrame.p - target.Head.Position).unit
tweenService:Create(neck, TweenInfo.new(0.25),{C0 = neckOrgC0*CFrame.Angles(0,(((headPos-target.Head.CFrame.p).Unit):Cross(TrsoLV)).Y*0.8, 0)}):Play()
tweenService:Create(headJoint, TweenInfo.new(0.15),{C0 = headOrgC0*CFrame.Angles(0, 0,-(math.atan(diff/dist)*0.8))}):Play()
else
tweenService:Create(neck, TweenInfo.new(0.25),{C0 = neckOrgC0}):Play()
tweenService:Create(headJoint, TweenInfo.new(0.15),{C0 = headOrgC0}):Play()
end
else
tweenService:Create(neck, TweenInfo.new(0.25),{C0 = neckOrgC0}):Play()
tweenService:Create(headJoint, TweenInfo.new(0.15),{C0 = headOrgC0}):Play()
end
end)