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

How Can I Restrict The Angles That My BodyGyro Affected Part Faces?

I figured out how to make an NPC's head turn towards another object, but it looks weird when the target object moves behind the NPC and the head goes into impossible positions and it looks creepy. How can I find the angles of where the head is facing so I can manipulate those angles into rotational restrictions?

local bg = script.Parent:WaitForChild("BodyGyro")
local head = script.Parent

while wait(0.5) do -- loop the movement
	-- maybe do some type of check before proceeeding and facing towards the target???
	
	bg.CFrame = CFrame.new(head.Position, Vector3.new(workspace.Lobby["Police Chief"].Head.Position.x, head.Position.y, workspace.Lobby["Police Chief"].Head.Position.z)) -- pretty much just makes the head look towards a police chief's (head).
end

Very simple script here, speaks for itself

1 answer

✓ Accepted answer

the head's CFrame's lookVector, or just

(head.Position - Vector3.new(workspace.Lobby["Police Chief"].Head.Position.x, head.Position.y, workspace.Lobby["Police Chief"].Head.Position.z)).unit

will tell you the direction. As this is a unit vector it is normalized to length 1. So that means, just get the component that is 'behind' and draw a threshold.

For example, if the policeman faces the x direction, then just make sure the x-component of the lookvector is >-0.2 or something. A lookvector x-component of 1 then means looking forward, -1 looking backwards.

Does this answer your question? I would use this method over using basePart.Orientation

while true do
  print(head.Orientation.Y)
  wait(0.1)
end

5 comments

0

Not completely, but I see how I can fix it, I'll accept as soon as I figure it out, thx!

0

sorry, i meant "if the NPC faces the x direction" not policeman

0

and u may wanna substract the head's position from the target instead of the other way arnound to get the lookvector X) Anyway correct by the lookvector of the rest of the body if it's not just always facing x (like in my example)

1

Hahahaha fantastic! I figured it out! I used the .unit for my answer, thank you for the help very much!!

0

awesome!

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