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

How to find the angle between 2 points. (Includes: CFrame, Angles math, Gun help?)

Asked by 6 years ago
Edited 6 years ago

Hello, this is not about correcting coding errors, but rather explaining what scripting methods I should use.

So, I am making a Dalek gun (From Doctor Who), which moves similar to a ball socket, very freely. I use Hinge Constraints to move the Dalek gun, I use TargetAngle to move it vertically, and the Attachment0's Orientation to move it Horizontally.

I'm focusing on moving it horizontally, to move it horizontally, the Vector3 is set like this.

(90,-90,0)

The Y-axis moves the gun sideways, and the X and Z-axis won't change. So, If I move the Y-axis to -45, it will move 45 degrees to the left, in a 45 degrees angle, it will do the opposite in -135.

Back to the question in the title. If I want the gun to point to a certain point. Point between the FirePart (Part where the gun spawns/fires the beam), and the point where my mouse is pointing (Either Mouse.Hit.p or Mouse.Origin.P). What would I use to find that angle, how, and why?

EDIT:

Woah, that almost put my question to rest. If you can help me with this bit, that's great, if not I'll 'Accept Answer' for getting me close to where I need to be.

Where is what I got for that:

gun.CFrame = CFrame.new(FirePart.p, Mouse.Hit.p) 

This teleports the part to where the gunpart in my dalek is and the front surface faces where the Mouse.Hit.p is.

How do I kinda get the Part to move with my lookVector. So, If I turn the Dalek (This would also make FirePart.p turn), the Part would make the orientation of FirePart, while matching the Mouse.Hit.p from that position. If that makes any sense.

Kinda like the gun.CFrame, + The orientation/lookVector of the FirePart while still pointing to the Mouse.Hit.p from the position in a sense.

1 answer

Log in to vote
0
Answered by
ax_gold 360 Moderation Voter
6 years ago
Edited 6 years ago

It's actually very simple, and can be done with one line of code. Just change the 2nd part of the CFrame to the mouse's position. Here's an example:

local mouse = game.Players.LocalPlayer:GetMouse()
local gun = workspace.Gun --(or something)

--The line below is the line with the example.
gun.CFrame = CFrame.new(gun.CFrame.p, mouse.Hit.p)

Notes:

-"p" in gun.CFrame.p and mouse.Hit.p returns the position of the object. It's no different than the Position value, but It should be used when referring to the mouse's position and with CFrames in general. It should not be capitalized, as it returns an error if it is.

-The values I used at the beginning of the script are examples so the script makes more sense. You should use your own values in your script.

now, as for the second part of your question, that's kinda tricky. You'd have to use a Ray to show where the firepart should go. A ray is basically an invisible line from one point to another. The link above explains it better than I can, so I recommend reading it.

That's all. Let me know if you have any more questions or if this doesn't work.

0
I re-edited my question, so I can clarify my question as much as possible. Thanks for the help. TheJellyNinja_XD13 62 — 6y
0
Ohh I get what you mean now. I'll add it to my answer. ax_gold 360 — 6y
Ad

Answer this question