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

Limit CFrame Rotation? [Not Answered]

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

2 comments

0

You don't need titles like "not answered." Solved threads get closed and get hidden from the lists. This post doesn't have have answers, so it shows right on the list "0 Answers".

0

I know. Just saying that because some people think that 1 answer means its solved...

1 answer

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

5 comments

0

...What? I'm sorry, I'm not this advanced in scripting. I don't even know where to put this, or how. .-.

0

From your script, it's not clear what it is that is "forward". So, if you can describe that, I can show you how to use this!

0

Idk what forward is. The position it's in IS forwards by default. so, its current position.

0

A position isn't a direction. Which is it allowed to point in that it has to be within 30 degrees of?

0

left and right, up and down...

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