How do i make a script where it prints what angle the part is directing to the TargetPart

Hi, im tryna make a angle script on the part and somehow i need it like to print the angle, but i don’t know what script line does it use.

also how do you like something like, if angle == 120 then (do something)

You might be surprised it’s not often we need to get the actual angle in games. If you can describe in more detail what you would like to do I can help. What angle do you want to retrieve? There are many angles relative to one part.

Maybe this will work for your example; Orientation is the angles of a part relative to the global axis.

if myPart.Orientation.Y == 120 then

it’d be best if you add a “<” or “>” symbol as it can be never on that specific number.

1 Like

No i mean like Something like, this script has a target right? depending on the parts rotation lets say the target is left, so that will be 60 degrees i think, but if the target is relatively straight the it will be 180 degrees how do i do this?

I’ve made a nice little graphic here for how to use the Vector3:Dot function to get if a target is left/right of a part’s facing direction. Like I said it’s rare to actually want angles, and even in that devforum post the author didn’t want an actual “straight” dot product, nor the angles, just if their target was left (negative dot product) or right (positive dot product). If you do just want to print the angles then use this, I am assuming since you mention target that you want the angle between where myPart is Looking and where the targetPart is.

-- points towards the target
local targetVector = (targetPart.Position - myPart.Position).Unit

-- Using dot product to get angle (in radians, then converted to degrees)
local dot = myPart:GetPivot().LookVector:Dot(targetVector)
print(math.deg(math.acos(dot)))

How do i make if some certain angle then do something? please help i have no time

Use the sample code, but again angles aren’t great at what they do, especially because they loop around 360.

-- points towards the target
local targetVector = (targetPart.Position - myPart.Position).Unit

-- Using dot product to get angle (in radians, then converted to degrees)
local dot = myPart:GetPivot().LookVector:Dot(targetVector)
local angle = math.deg(math.acos(dot))

if angle > 120 then
    print("here is where you put code")
end

Thank you! this will make my cars aim straight!

Sounds like that’s the exact case this devforum post wanted. How do i get the straight angle for DotProduct?

You will want this code instead (not using angles)

-- points towards the target
local targetVector = (targetPart.Position - myPart.Position).Unit

-- Using dot product of right vector to get negative/positive values
local dot = myPart:GetPivot().RightVector:Dot(targetVector)

if dot < 0 then
    print("turn left")
elseif dot > 0 then
    print("turn right")
else
    print("straight! (vs right vector)")
end