I'm trying to make a part only face the target's direction only by the X-Axis while the part is moving towards the target with tweening. I tried mashing 2 functions together (the tween moving function and direction facing function) but obviously, it didn't work.
Code:
faceTarget = function(target, torso) if target ~= nil then local BG = Instance.new("BodyGyro") BG.MaxTorque = Vector3.new(0, math.huge, 0) BG.P = 7500 -- // The point of this is to set the speed of how fast the AI turns BG.Parent = torso end end chaseAfter = function(zombie, torso) while true do wait() local target = findNearestTorso(torso.Position) if target ~= nil then local info = TweenInfo.new(math.random(10)) local goal = {} goal.CFrame = target.CFrame faceTarget(target, torso) local tween = tweenService:Create(torso, info, goal) tween:Play() end end end
Well using CFrame, to face in the direction of an object you would do...
CFrame.new(position, lookAtPosition)
So if you wanted to limit it to only the X-Axis, it would be...
-- Something similar to CFrame.new(position, Vector3.new(target.Position.X, part.CFrame.lookVector.Y, part.CFrame.lookVector.Z)
And you could actually include that in the tween by doing...
... local goal = {} goal.CFrame = CFrame.new(target.Position, Vector3.new(target.Position.X, part.CFrame.lookVector.Y, part.CFrame.lookVector.Z) ...
What you could do is set the BodyGyro's MaxTorque to: Vector3.new(math.huge, 0, 0)
, so force is only applied on the x-axis.
Then set the bodyGyro's CFrame to: CFrame.new(torso.Position, target.Position)
, when you want the zombie AI to face the player