Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
1
Answered by
Nonaz_jr 439 Moderation Voter
6 years ago

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

0
Not completely, but I see how I can fix it, I'll accept as soon as I figure it out, thx! laughablehaha 494 — 6y
0
sorry, i meant "if the NPC faces the x direction" not policeman Nonaz_jr 439 — 6y
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) Nonaz_jr 439 — 6y
1
Hahahaha fantastic! I figured it out! I used the .unit for my answer, thank you for the help very much!! laughablehaha 494 — 6y
0
awesome! Nonaz_jr 439 — 6y
Ad

Answer this question