So this script makes a Part follow you're mouse. Thing is, I dont want the part to be turning to the mouse if it's pointing 180 degrees of that part. I would like to limit it like, 30 degrees to the left and right (Doesnt need to be exactly 30, just some number that limits the parts CFrame Rotation.)
local Player = Game.Players.LocalPlayer local Mouse = Player:GetMouse() while true do local person = Player.Character if person ~= nil then local neck = Player.Character.ATST.Head.Neck local looking = CFrame.new ( neck.Position, Mouse.Hit.p ) neck.Neck.C0 = neck.CFrame:toObjectSpace(looking) end wait() end
Here's a simple function I wrote that limits a vector v
to be within a certain angle
(in radians) to vector d
.
function coneLimit(v,d, angle) v , d = v.unit, d.unit; local dot = d:Dot(v) local toward = dot * d; local side = v - toward; local outtoward = math.max( math.cos( angle ) , dot) * d; local om = outtoward.magnitude; local sm = side.magnitude; -- (Q * sm)^2 + om ^ 2 = 1 -- sqrt(1 - om^2) / sm = Q local Q = math.sqrt(1 - om^2) / sm; local outside = side * Q; return side * Q + outtoward; end
Essentially, this constructs a triangle using two components: the component forward (in the direction of d
) and the component to the side (side
).
It restricts the forward to have to be forward by at least a certain amount (outtoward
) and then sums that with a scaled side
(by Q
) to use the right proportion of each to make a unit vector.
A hypothetical use would be the following, to restrict a headlookvector
to be within 30 degrees of a torsolookvector
.
headlookvector = coneLimit( headlookvector, torsolookvector, math.rad(30) )