Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make the part face the target's direction by only the X-Axis while tweening?

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

2 answers

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

1 comment

0

it worked, but the only problem is that the zombies are facing backwards...

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

1 comment

0

didnt work

This discussion is preserved as it was. New answers, comments, and votes are closed.