Skidmark generator scripts in Unity that I’ve found in the past either seem to have bad performance, or they’re trail renderers where the whole thing disappears as soon as the car stops skidding.
I’ve made one that’s kind of based on the really old official car tutorial’s one, but instead of insane garbage generation that uses up quarter of your CPU in a frame every half second, it generates no garbage. Performance is good unless you’re doing something insane.
Update in 2021: Please note that as of 2021 this project still works, and feel free to use it, but I won’t be maintaining it or providing support for it any longer.
I’ve never actually used the 2D side of Unity. If it’s just basically a camera looking straight down it should work. You control when and where the skidmarks generate, and they’re just a mesh.
Edit: The previous post has been edited, and I was replying here to it originally reading: “Hi, your script works great except one thing, marks are under terrain and distant.”
Hmm, I use the same script on Unity terrain with no trouble, but I haven’t tested it in recent Unity versions. Does it look correct if you run the Sample Scene?
It looks like you’re holding the handbrake on those turns? Or maybe wheel physics have changed a little in the latest Unity versions. The physics on that car are pretty bad anyway honestly - it’s a default Unity sample car. I wouldn’t recommend using it in a proper game, it’s just in there to show how the skidmarks work.
Re skidmarks on turns. Check out WheelSkid.cs in the project. It’s pretty simple and commented. Note how the skidTotal is calculated as a sum of the sideways slip (car not going the way it’s facing) and wheel spin (loss of traction). You could remove or modify the sideways slip calculation if you like.
// NOTE: This extra line should not be needed and you can take it out if you have decent wheel physics
// The built-in Unity demo car is actually skidding its wheels the ENTIRE time you're accelerating,
// so this fades out the wheelspin-based skid as speed increases to make it look almost OK
wheelSpin = Mathf.Max(0, wheelSpin * (10 - carForwardVel));
@Wenon I’m really glad you’re able to discern which way to move which number. I didn’t have a clue. My math skills are in minus territory. I was purely entertained (and drawn to this line) by the fact it’s not necessary and so pointedly/humorously commented. I didn’t even dare suggest which part of it to try changing to get a desired result. It’s Egyptian hieroglyphics, to me.
@Nition you deserve an award for figuring out the problems and how to solve them. I have been completely mystified by the Wheel Collider. It seems like a bad idea, badly designed and badly implemented.
Maths is one thing, but don’t ever feel bad that you can’t get the wheel colliders to work - they’ve been almost unusable since the PhysX update in Unity 5.0. You’re either sliding on ice or jittering off into space with anything but the most generic vehicle.
In case you’re even thinking of feeling bad that you can’t get the wheel collider to work for you, consider that:
The official example vehicle is constantly losing traction when accelerating at any speed. Unity Technologies themselves couldn’t make it work right.
Even Edy, the creator of the most popular vehicle package on the Asset store, couldn’t get them to work without disabling most of what they usually do. [(source)]( Unity 5. WheelCollider component is unusable. page-6#post-2077136)
Funnily enough the Unity 4 version of the wheel colliders was actually not bad.
Great Job Nition! It’s almost a drop in replacement for Realistic Car Controller’s SkidMark system - except now rather than 190KB of GC every frame (lots of vehicles), I’m now getting 0B… Thanks!
Thanks for sharing this script. I’ve got a small problem with using it. The side marks are way off the position that should be.
Vector3 localVelocity = transform.InverseTransformDirection(m_Rigidbody.velocity);
float skidTotal = Mathf.Abs(localVelocity.x);
float wheelAngularVelocity = m_WheelColliders[i].radius * ((2 * Mathf.PI * m_WheelColliders[i].rpm) / 60);
float carForwardVel = Vector3.Dot(m_Rigidbody.velocity, transform.forward);
float wheelSpin = Mathf.Abs(carForwardVel - wheelAngularVelocity) * WHEEL_SLIP_MULTIPLIER;
// NOTE: This extra line should not be needed and you can take it out if you have decent wheel physics
// The built-in Unity demo car is actually skidding its wheels the ENTIRE time you're accelerating,
// so this fades out the wheelspin-based skid as speed increases to make it look almost OK
wheelSpin = Mathf.Max(0, wheelSpin * (0 - carForwardVel));
skidTotal += wheelSpin;
if (skidTotal >= SKID_FX_SPEED)
{
float intensity = Mathf.Clamp01(skidTotal / MAX_SKID_INTENSITY);
Vector3 skidPoint = wheelHit.point + (m_Rigidbody.velocity * (Time.time - lastFixedUpdateTime));
lastSkid = skidmarksController.AddSkidMark(skidPoint, wheelHit.normal, 1, lastSkid);
}
else
{
lastSkid = -1;
}
That code you pasted is just part of WheelSkid.cs that comes with the project. Do the skidmarks show in the right place when you’re running the default test scene that comes with it, before you’ve made any changes?
Check where your wheelHit.point actually is - does it match your wheel positions?
Try removing the velocity component, just setting skidPoint = wheelHit.point, and see where it ends up then.
Okay, I’m out of ideas then I’m afraid - something’s different between yours and the original test scene. Maybe post here if you manage to work it out, just in case others have the same issue in the future.
Ah, that’s not that stupid, I should be allowing for that or at least putting in a note that it needs to be at 0,0,0. Thanks for bringing it up. I think Wenon earlier in this thread probably had the same issue when they said "your script works great except one thing, marks are under terrain and distant", but they edited their post to something else after fixing it.