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:

If you can help, please help me…

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
3 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 :smiley:

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

Thank you all for your hard work, if i can somehow help you, tell me!

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!
I had no such idea!

(30 characters bruhhhhh)