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

Angle between 2 vectors changing based on distance?

Asked by 4 years ago

So i'm trying to get an angle from 2 vectors (one of them is the mouse.hit position, the other is the player's right hand), but im having this issue where the angle changes based off of the distance between the 2 vectors. For example if im looking forwards at a part that is 10 studs infront of me it might say an angle of 2 but if i walk to the side without even moving my mouse but the mouse is no longer hitting that part and is instead hitting a part say 100 studs away it would change to an angle of 50 eventhough the distance between the 2 vectors should not be affecting the angle in any way.

This is the math:

math.deg(math.acos(RightHand.Position:Dot(Mouse.Hit.Position)/(RightHand.Position.Magnitude * Mouse.Hit.Position.Magnitude)))
0
uhm, quick question, why are you doing this? I think it would be a lot easier to use CFrames. iHavoc101 127 — 4y
0
Unless I'm thinking of something totally different here iHavoc101 127 — 4y
0
i have to get the angle so that i can use it with the Motor6D:SetDesiredAngle(number) function which only works with a singular number, not an entire cframe Oxygen4Lyfe 408 — 4y
0
This might have something to do with positions based on world position vs. relative positions. You should use CFrames for this, and if you're doing what I think you are, moving the arm to look at the mouse then I can give you a few pointers for this. radiant_Light203 1166 — 4y
0
thats exactly what im trying to do Oxygen4Lyfe 408 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

As stated in a comment, you want the arm to point where the mouse goes. Here is the script that I use, but without remote-events and it misses alot of the necessary variables. I'll explain what you need and the problems with the script, but I don't have Studio right now so I can't confirm anything.

local Shoulder = Character.Torso["Right Shoulder"]
local ShoulderOffset = CFrame.new()
local Mouse = Player:GetMouse()

game:GetService("RunService").RenderStepped:Connect(function()
    local MouseLookVector = Mouse.LookVector
    Shoulder.C0 = ShoulderOffset * CFrame.Angles(0,0,0) * CFrame.Angles(-math.asin(MouseLookVector.Y),0,0)
end)

ShoulderOffset isn't actually a blank CFrame. You can get the actual offset by printing the C0 of the RightShoulder Motor6D in a Dummy. You'll need to use the command bar for this.

I haven't defined Player, Character so you'll need to do those.

In the first CFrame.Angles. One of the values will be pi/2 or 90 degrees because otherwise the arm won't be pointing straight at the mouse and I forget which axis it needs to be offset on. Similarly, it is possible that the X axis isn't the proper axis to rotate the arm on, in which case you can try switching it to the Y or Z axis. It's also possible that there shouldn't be a minus because otherwise the arm will move opposite the mouse (pointing up will move the arm down); interesting thing to note is that if the right arm has a negative arcsin then the left arm will have a positive one.

This won't replicate to the server, you'll need remote events for those.

There won't be a way to stop this you will have to script it yourself but it should be easy.

0
The closest to working that i can get this is by not offsetting anything in the first cframe.angles and removing the negative sign inside of the 2nd cframe.angles. This has the problem of it not aiming at the center of the screen, as in it is offset by a few degrees. Do you know how I can fix that part of it? Oxygen4Lyfe 408 — 4y
0
Whoops, I forgot to check the comments. Could you send a picture? Because it should be pointing at the mouse even if it doesn't look like it. radiant_Light203 1166 — 4y
Ad

Answer this question