You can’t use friction direction 2 in 2D space yet [citation needed], so I use this one hack in combination with hingejoints and directionjoints to create a realistic top-down vehicle. For some reason though, after driving in a perfectly straight line for some time, the car spazzes out and just starts acting weird. Seems NOT to be an issue with joints not preventing wheels from rotating, but they might be responsible for glitching in combination with the script.
Here’s my issue in detail.
http://www.reddit.com/r/Unity2D/comments/2segao/topdown_car_issues/
Here’s my setup. (floatiness got set to 0 instantly after taking the screenshot, it was just a shot in the dark).
wheel.js
#pragma strict
var relativeVelocity : Vector3;
var floatiness : float;
function FixedUpdate () {
relativeVelocity = transform.InverseTransformDirection(rigidbody2D.velocity);
relativeVelocity.x *= floatiness;
rigidbody2D.velocity = transform.TransformDirection(relativeVelocity);
}
Here’s what happens when I drive it forwards and backwards, just applying constant positive and negative force at transform.up of the car’s GameObject.
Here’s the state of joints after the ride. (perfectly intact and keeping the wheels nicely alligned)
Camera and crosshair don’t have a rigidbody. They’re parented to the car. Not much else to comment on.
At first I thought it was faulty joints at play, so I made them as rigid as possible, seems not to have solved the issue. When I floor it after the “after state,” it goes sideways at random, thing is obviously broken even though joints are visibly intact.
So I think the issue has got to be the script, but I can’t see anything wrong with it. Uses inversetransformposition to pick up relative velocity, kills the X but not really (so skidding is possible)(as I’ve said, floatiness is set to 0, so for all purposes, yes really), and yet ageenvenvejbvešťšľťľč+ľťž.
As for relativeVelocity, when I monitor it, X is constant and safe at 0 at all times, even though the car is obviously going sideways and wheels are obviously perfectly alligned even in motion.
When I drive slower/apply smaller force, it just takes longer to kick in. But the distance travelled at which it starts breaking is about constant.