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

How do I find the angle between two lookVectors or bricks?

Asked by 6 years ago

Alright,

Let's just first say that (if you wouldn't understand it from the question) I'm a bit of a noob to scripting. I know the basics but not much more so please explain what your code/answers mean in simple language.

Now to my question; I've been searching the internet far and wide and had trouble really finding a good answer, so I'm asking here: Is there a good simple way to check the angle between two objects or lookVectors. What I've found is that people seem to be using math.atan, and since I don't know trigonometry I have no idea how to use this myself. I'd be super happy if someone could either generally explain what ways there are to do this or explain to me how the more complicated ways work. I tried converting a lookVector using toEulerAnglesXYZ() and whatever I did didn't work, so if you think this could be useful for me, I'd love it if you described how to use it.

Thanks in advance! :D

0
You can use the constructor of CFrame to create a CFrame at a position looking towards another ie CFrame.new(position vector3, look at position vector3)  User#5423 17 — 6y
0
Look up "EgoMoose" Validark 1580 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

So I actually managed to solve this myself. After doing some more digging on the wiki and devforums I managed to put together this piece of beautiful code:

function findRotation()
    local direction = game.Workspace.Camera.CFrame.lookVector
    local heading = math.atan2(direction.x, direction.z)
    heading = math.deg(heading)
        heading = (heading + 0.5) - (heading + 0.5) % 1
        heading = heading + 180
    print(heading)
end

Whenever the findRotation() function is called it will print a number from 0 to 360. This number is of course dependent on where you look. Looking straight forward will give 0, 90 degrees to the right will give 270, 90 to the left will give 90 and so on. I hope this helped any future noobs looking to make their own compass or anything else that you want to use this for!

//LetsGoRobloxia

Ad

Answer this question