I am trying to code a creature which will look at you, but I would like its head not to turn upwards or downwards, only side to side
This may look fine at first, but things get weird once I orientate it.
The Green arrow indicates where it should look
The Neck turns based off the world’s axis, and since i am terrible at cframing, i honestly don’t know what to do to make it so it turns relative to its torso.
Code:
local function worldCFrameToC0ObjectSpace(motor6DJoint,worldCFrame)
local part1CF = motor6DJoint.Part1.CFrame
local c1Store = motor6DJoint.C1
local c0Store = motor6DJoint.C0
local relativeToPart1 =c0Store*c1Store:Inverse()*part1CF:Inverse()*worldCFrame*c1Store
relativeToPart1 -= relativeToPart1.Position
local goalC0CFrame = relativeToPart1+c0Store.Position
return goalC0CFrame
end
local goalCF = CFrame.lookAt(Head.Position, Vector3.new(Target.Position.X, Head.Position.Y, Target.Position.Z), UpperTorso.CFrame.UpVector) * CFrame.Angles(math.pi, math.pi, 0)
NeckJoint.C0 = worldCFrameToC0ObjectSpace(NeckJoint, goalCF)
do
local Game = game
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
local Root = Character:FindFirstChild("HumanoidRootPart") or Character:WaitForChild("HumanoidRootPart")
local RigType = Humanoid.RigType.Name
local Neck
if RigType == "R6" then
local Torso = Character:FindFirstChild("Torso") or Character:WaitForChild("Torso")
Neck = Torso:FindFirstChild("Neck") or Torso:WaitForChild("Torso")
elseif RigType == "R15" then
local Head = Character:FindFirstChild("Head") or Character:WaitForChild("Head")
Neck = Head:FindFirstChild("Neck") or Head:WaitForChild("Torso")
end
if not Neck then return end
local function OnRenderStep()
Neck.C0 = CFrame.new(Neck.C0.Position) * Root.CFrame.Rotation * CFrame.Angles(math.pi / 2, math.pi, 0)
end
RunService.RenderStepped:Connect(OnRenderStep)
end