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

Improve my math to make it more accurate please?

Asked by 10 years ago

I am trying to make the character's arms follow the CurrentCamera with SetDesiredAngle and some fake arms. I made some gimmicky formula thingy to set the angle correctly, but sometimes the math is off (Camera looks down, arms go up). Could someone improve my formula? I've heard that trigonometry could solve the problem but I'm not exactly sure how to get side lengths (my only experience with trig is basically 5 lessons of Khan Academy).

repeat wait() until game.Players.LocalPlayer

frames = setmetatable({},{})
player = game.Players.LocalPlayer
character = player.Character
cam = Workspace.CurrentCamera

frames.Default = {
    Right = CFrame.new(1.5,-1,-1.5)*CFrame.Angles(math.rad(90),0,0);
    Left = CFrame.new(-1.5,-1,-1.5)*CFrame.Angles(math.rad(90),0,0)
    }

frames.Gun = {
    Right = CFrame.new(1,-1,-1)*CFrame.Angles(math.rad(90),0,0);
    Left = CFrame.new(-0.5,-1,-1.5)*CFrame.Angles(math.rad(90),0,math.rad(30))
}

getmetatable(frames).__index = {}
getmetatable(frames).__index.CurrentFrame = frames.Default

rarm = character["Right Arm"]
larm = character["Left Arm"]
frarm = rarm:Clone()
flarm = larm:Clone()

frarm.Parent = cam
flarm.Parent = cam

player.CameraMode = "LockFirstPerson"

game:GetService("RunService").RenderStepped:connect(function()
    frarm.CFrame = cam.CoordinateFrame*frames.CurrentFrame.Right
    flarm.CFrame = cam.CoordinateFrame*frames.CurrentFrame.Left
    character.Torso["Right Shoulder"]:SetDesiredAngle(math.rad(frarm.Rotation.X)-(math.rad(frarm.Rotation.X)-math.rad(90))*2) -- Math for Right Arm
    character.Torso["Left Shoulder"]:SetDesiredAngle(math.rad(-flarm.Rotation.X)+(math.rad(flarm.Rotation.X)-math.rad(90))*2) -- Math for Left Arm
end)
0
The website jacked up the script lol. evolvedpikachu 40 — 10y
0
Which is why you use the little "Code Block" button. Select it all and click the little "Lua" logo and then it comes out as a script. Tempestatem 884 — 10y

1 answer

Log in to vote
1
Answered by
MrNicNac 855 Moderation Voter
10 years ago

Hi evolvedpiakchu,

Back in some twisted mind state I was in at a time - months ago - I decided to create a solution for example this. Here is the code and brief explanation that I made.

-- Expects character to already be there (does not check for nil or wait for character)
-- Needs Animate script removed
local p = Game.Players.LocalPlayer
local m = p:GetMouse()
local joint = p.Character.Torso['Right Shoulder'];

function AngleBetween(v1, v2)
    local d = v1:Dot(v2);
    local angle = d/(v1.magnitude * v2.magnitude)
    return math.acos(angle);
end

m.Move:connect(function()
    local Omega = (m.hit.p - p.Character.Torso.Position).unit;
    local Omega_c = Vector3.new(Omega.X, Omega.Y,0);
    local Theta = AngleBetween(Vector3.new(0,-1,0), Omega_c);
    joint.DesiredAngle = Theta;
    print(math.deg(Theta));
end)

So basically, taking in the comments already written, this will connect the Move event of the mouse to make the angle degree as if it were "pointing" the arm at the camera.

The math is trigonometry, yes. If you haven't taken trig, then it will be a little complicated to "think up" a solution for problems like these. Those who have taken the class (and listened) should easily be able to think this through.

I basically take two vectors (one as the world-space negative normal and one as the vector pointing with the camera) and fine the angle between them. This is all that is needed for the DesiredAngle to function correctly.

The actual mathematical concepts are more in-depth than a Lua-based question should answer. That would be for math classes.

0
Huh, thanks. I knew it had something to do with cos and finding the angle between the arm and the camera but I wasn't really sure how to do that. I haven't learned what a dot product is or what omega stands for either. I hope 8th grade math covers this next year :D evolvedpikachu 40 — 10y
Ad

Answer this question