Hi! Wall of text incoming, sorry
To make the skateboard turn you have to change the direction/angle somehow, and make most or all of the forwards velocity go into the new direction. Skateboards turn kind of like four wheel drive cars, in that all fours wheels turn out of line with the board, causing the rolling direction of the wheels to be out of line with the board, with the front and back wheels turning in opposite directions. This makes the board steer in a circular arc instead of straight ahead.
Here’s a (not super relevant but cool) video showing how leaning turns into turning the wheels, which causes steering:
There’s countless ways you could implement steering, but if you want it to “feel real”, it makes sense to mirror the way you steer a skateboard IRL, i.e. by leaning left/right. More lean = tighter circular arc = more curvature = harder steering. On a skateboard you can control the amount of lean precisely because it’s just a matter of shifting your weight around, so it’s kind of hard to make it work with e.g. pressing A/D on a keyboard, because that doesn’t give a range of steering, only full steer left or right, or no steer at all. On a controller it might make sense to use an analog stick to control lean. One way you could have decent leaning on keyboard is to have the lean slowly grow while you hold A/D, so it takes like a second to get up to the tightest turns. Obviously this give less control and is less like real skating, but hey what can you do? Maybe add a modifier so that just pressing A/D builds up to half lean, and pressing left shift at the same time builds up to full lean?
Anyway, no matter how you control the amount of lean, that needs to be translated into some amount of turning. Since a given lean angle equates to some wheel angle, and since a constant wheel angle makes the skateboard follow a circular arc of constant curvature, the skateboard just needs to turn by some number of degrees per second for a given lean angle.
So there is some formula that converts any lean angle into a number of degrees per second of turning that accurately simulates skateboard steering.
Here’s an article that talks a bit about how to convert this lean angle into a steering angle:
Apparently it can depend on a couple of factors like how the hardware is built, but one example it gives of a “steering curve” is a linear function, so if we use such a function then that’s realistic for at least some skateboard setups. A linear function looks like this:
f(x)=a*x
In this case x
is lean angle, f(x)
is steering angle in radians (or degrees) per second., and a
is just some factor that determines the slope of the function, or how responsive the skateboard feels to steer. a
just needs to be tuned to something that feels good. It’d be cool to let players customize all of those factors, but for a first iteration let’s just go with that function.
So in summary, you need to figure out a way to determine lean angle, and we can convert that to steering angle in radians per second by multiplying the lean angle by some number that we tune. Steering responds pretty much instantly to leaning, so it’s probably not necessary to use a PID controller for the steering angle. It might make the controls feel better to use a PID controller to convert controller or keyboard into lean angle however.
Once you can determine the steering angle, you need to somehow control the angular velocity of the skateboard, which you can do in a bunch of ways. Parts have a property called AngularVelocity that you can set directly, which seems to me like it’d work just fine. Another approach is to use a Torque, which is like a VectorForce is to (linear, or just “normal”) velocity. I.e. applying a torque makes the angular velocity grow over time. But if the controls should feel snappy and realistic instead of floaty and unresponsive, I think just setting AngularVelocity would be better.
There’s a bunch of ways to make the system I described better, like adding skid / drifting when you steer sharply at high speed. I think it makes sense to get the basics down first tho.
Hope this helps, if you have any questions feel free to ask I don’t mind writing some example code either, but I might not have time right away.