Bodygyro to make a block face a player

i am trying to make it so as a player moves the front face of a part will constantly be facing the player, i tried changing the cframe of the bodygyro by doing:

bodyG.CFrame = CFrame.new(part.Position,newpart.Position)

however this holds little to no effect, i am currently testing it on the regular base sized part if that holds any importance. Am i doing this wrong if so how do i fix this.

Thanks in advance

Make sure you crank up the MaxTorque of the gyro.

how high should i go and if so should i raise the power

High torque will make the any external force very hard to counteract the gyro. Power is the aggressiveness of the turning, and dampening slows the gyro down before it reaches its destination.

It’s a matter of experimenting. Torque is what you need to change if the brick isn’t moving very much. Power and dampening are more for visual preference.

its beginning to work now, however this is still a large problem. This is all for a follow system towards the player and if the player walks straight through the brick it now treats the back surface as the front surface is this something to do with the gyro or something completely unrelated?

This might be when the lookAt CFrame constructor tries to look straight upward or downward. There are ways around this, but it will take a little bit more complicated approach.

Does it permanently break it after the player leaves the brick?

nothing gets permanently broken all that happens is the surface that looks at the player changes, the brick does not have the capacity to do a 180 turn instead it just decides to change the surface in which follows the player. It can be fixed if the player goes over the brick making the front surface the main surface. This does not happen if the player does a wide turn only if the player is trying to force a quick 180 turn by walking through the brick.

Can you verify that the surface is actually changing? Or if instead it is the same surface but quickly flipped before turning 180 degrees back toward the player again.

yeah i go in the properties and highlight front surface and i can confirm that it does not do a quick 180 it just flips which surface is in control.

Not sure if this is useful to you, but CFrame.new() has a second argument useful for making parts look towards the direction of another part.

CFrame.new(Object, LookAtTarget)

How to use it
local Part_To_Face = game.Workspace:FindFIrstChild("Part1")
local Target_To_Face = game.Workspace:FindFIrstChild("Part2")

Part_To_Face.CFrame = CFrame.new(Part_To_Face.Position, Target_To_Face.Position)

This should face Part_To_Face towards Target_To_Face.
You can read up more on this here
CFrame

I know you were trying to use a BodyGyro, but perhaps this’ll be helpful!
Good luck!

2 Likes

yeah i’ve got that its just the fact that its not flipping being the problem

My bad, I’m quite dumb. You literally had what I stated within your code.

1 Like

I made an example of this, a look bot that looks at humanoids.

--settings
local Dampening = 600 --Decays the motion. Little to none means springy shakey, a lot makes motion subdued and lazy.
local MaxTorque = 10000 --Maximum force to rotate. Super high might screw with the hinge constraint.
local P = 8000 --Willingness to use that force. Raising it will make a faster head turning.

--body
script.Parent.Anchored = false --physically simulate the hinge head
script.Parent.Anchor.Transparency = 1 -- hide the anchor on runTime

script.Parent.BodyGyro.D = Dampening
script.Parent.BodyGyro.MaxTorque = Vector3.new(MaxTorque,MaxTorque,MaxTorque)
script.Parent.BodyGyro.P = P

local function lookAtNearest()
	--get nearest head in model that contains a humanoid
	local nearest
	local distanceOfNearest = math.huge
	for index,child in ipairs(workspace:GetChildren()) do
		if child.className == "Model" and child:FindFirstChild("Humanoid") and child:FindFirstChild("Head") then
			local candidateDistance = (script.Parent.Position - child.Head.Position).Magnitude
			if candidateDistance < distanceOfNearest then
				distanceOfNearest = candidateDistance
				nearest = child
			end
		end
	end	

	--look at it if it exists	
	if nearest then
		local dir = (nearest.Head.Position - script.Parent.Position).unit
		local spawnPos = script.Parent.Position
		local pos = spawnPos + (dir * 1)
		script.Parent:findFirstChild("BodyGyro").cframe = CFrame.new(pos,  pos + dir)
	end	

end

--checkLoop
while true do
	wait()
	lookAtNearest()
end

This code just shows a part of the system. Full thing below.

1 Like