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

Limit CFrame Rotation? [Not Answered]

Asked by 9 years ago

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
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". BlueTaslem 18071 — 9y
0
I know. Just saying that because some people think that 1 answer means its solved... Orlando777 315 — 9y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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) )
0
...What? I'm sorry, I'm not this advanced in scripting. I don't even know where to put this, or how. .-. Orlando777 315 — 9y
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! BlueTaslem 18071 — 9y
0
Idk what forward is. The position it's in IS forwards by default. so, its current position. Orlando777 315 — 9y
0
A position isn't a direction. Which is it allowed to point in that it has to be within 30 degrees of? BlueTaslem 18071 — 9y
0
left and right, up and down... Orlando777 315 — 9y
Ad

Answer this question