# Neading help for detecting stearing angle

Hello this is my first post, i nead some help.
I want to build simple NPC car (NPC ride it player cant) with stearing to points.

One wheel at back is power wheel, foward wheels are connected with servos to car,
i nead to get them to some angle to move to points (like real car).

Dont wory i tested model while manual steering it and it mowed where i wanted.
So far i have made car steer with no oscilations and that kind of garbage bugs,
but i cant get it to right angle to steer.

Script that i used:

route=script.Parent.Parent.ROUTE
step=1

repeat wait(2)
y1=script.Parent.Center.Position.Y
z1=script.Parent.Center.Position.Z
destination = route[â€ś1â€ť]
y2=destination.Position.Y
z2=destination.Position.Z

``````local dy = y2 - y1
``````

local dz = z2 - z1
local angle =math.deg( math.atan2(dy, dz))

``````script.Parent.HingeConstraint.TargetAngle=angle
script.Parent.HingeConstraint2.TargetAngle=angle

print(angle)
``````

until 2>3

Script works well to some point but then i get this:

Sorry for broken Englishâ€¦

1 Like

try using

``````local unit = (destination.Position - thisCar.Position).Unit
local angle = math.atan2(unit.x, unit.z) -- (might end up being unit.z, unit.x)
HingleConstraint.TargetAngle = angle``````
2 Likes

I got mixed resultsâ€¦
When is going towards X cordinate line, it works

But when it goes to Zâ€¦

ignores itâ€¦

i tried to flip math.atan2(unit.X, unit.Z), but nothing happendâ€¦
and i tried this angle=math.deg( math.atan2(unit.X, unit.Z) ) but i gotâ€¦

it going aroundâ€¦

1 Like

I will give ya car model if you want to test it or just take it
test car model.rbxm (1.4 MB)

1 Like

ok, im gonna check your code!

(30 characters)

1 Like

It is hardâ€¦
It may take time

1 Like

No problem, its not something extra important or something like that, take your time and go easy

Your code (with `math.deg`) inherently is a good idea, but youâ€™re calculating the angle from only positions, which realistically does not work, since if the car is rotated, the target angle has to adjust accordingly. This is why the car points into the right direction at the very beginning, but doesnâ€™t update its wheels during motion. To â€śfixâ€ť your current solution, just adjust the angle according to the car orientation.

``````angle = math.deg(math.atan2(unit.X, unit.Z)) - thisCar.Orientation.Y+180
``````
2 Likes

Cool!

I maked auto pilot using @emojipasta code!

test car model.rbxm (2.9 KB)

I was stupid to forget thatâ€¦ anyway thanks man

did you send right model? i get from your model just route model?

Woops, i send wrong fileâ€¦

test car model.rbxl (101.9 KB)

This is correct one

1 Like

Wow this looks nice, few seconds later car dies i have to modify this, thank you anyway

You can delete

``````script.Parent:BreakJoints()
``````

to protect car lol

I know but it looked funnyâ€¦
Just wroom to last point and then brek

may i also ask what effect does #destination have on destination value?
(what # does?)

#destination:GetTouchingParts() returns a number.
This will return the number of parts the destination is touching.

i did small modifcation on script so it dont destroys parts on contact, destroys them on distance, i will use this with path finding service
code modification:
if (destination.Position-script.Parent.Center.Position).magnitude <= 1 then

Cool!