Shopkeeper's Head Will Not Rotate Correctly

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want COBALT (the shopkeeper) to look at the nearest character to him.

  2. 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.


  3. 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:
image

3 Likes

Couldn’t you just do

 neck.C0 = CFrame.lookAt(neck.C0.Position,char.Head.Position)


1 Like


Nope, it’s even worse now. He looks behind him and never turns his head now.

replace the neck.c0 position with the head of the robots position

image
Nope, now he’s been decapitated.

I think it’d be better to just supply the rig for better understanding.
cobalt.rbxm (736.2 KB)

bump, still struggling with this.

bump again, been banging rocks together for about an hour doing this, my head hurts.

A Motor6D’s C0 and C1 are in object space. The char.Head.Position vector should be converted to that object space first.

For example you can convert the head position vector to be in the object space of the part that the head connects to:

neck.C0 = CFrame.lookAt(neck.C0.Position, neck.Part0.CFrame:PointToObjectSpace(char.Head.Position))

This however doesn’t take to account any possible edits done to the C1 of the neck Motor6D.

thanks for the help, but i’ve already figured it out.

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)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.