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

How to make camera move with gun?

Asked by 8 years ago

Can someone give an idea on how you would make it so when the player looks up, so their gun/item. I think it might have to do with the lookvector but I don't know how to use it! I don't even know where to start with this, I'm not asking for the code at all either, just a brief explanation on how you could possibly do this.

1 answer

Log in to vote
2
Answered by 8 years ago

The first thing you need for this is a vector describing the direction you want to face (i.e. from your shoulder to the mouse position). Read up on how vectors work here, if you're unsure.

You can do this by saying local directionVector = (mousePosition - torsoPosition).unit. I'll leave it up to you to find those two positions (note I substituted torso position for shoulder position; it's close enough).

Now you need the vertical angle of this. Some simple trig will tell you this can be done by doing local angle = math.asin(directionVector.y).

Now just set the angle of the shoulder to that, and you're good to go. Psuedo code below:

--these are up to you to find:
local torsoPosition, mousePosition;
local shoulder;
local defaultShoulderC1;

--the fun stuff
local dir = (mousePosition-torsoPosition).unit
local angle = math.asin(dir.y);

shoulder.C1 = defaultShoulderC1 * CFrame.Angles(angle, 0, 0); --that may be negative or even a different axis depending on how ROBLOX decided to attach arms.
Ad

Answer this question