- Home /
Want to add a "drafting" feature to my racing game.
I am doing a very very simple racing game. It is 3d but basic. I am new to Unity and game in general. I have tried using the wind feature, angular drag, and a host of other features. I am assuming it will be some complicated script to "add force" when in a certain range of the vehicle. If anyone can point me in the right direction, I would really appreciate it.
Answer by AlwaysSunny · Dec 06, 2016 at 11:16 AM
Actually a pretty interesting and complex topic. You have two stages: Determine whether to apply this effect to this car this frame, and determine what behaviors to apply. I assume by talking about forces, you're actually moving these cars with rigidbody physics and methods like AddForce, correct?
First determine whether to apply the effect. You could have a box collider which extends from the rear of every vehicle. It could optionally scale with velocity, growing in length to encompass a greater area. Additionally (or alternatively) you could include raycasts, distance checks, angle checks, etc to discover that you're actually in the cone-like area behind the vehicle which would create drafting conditions. A fan-like array of raycasts might work well instead.
Once you know you should apply drafting this frame, your data from the previous steps - and additional checks for e.g. angle difference between vehicles - can inform the force vector you want to apply, or otherwise influence the behavior of the vehicle. Once you know you're in the zone, it's just math. Actually it's all math. :D
I'd probably ignore the box collider part / fan-of-raycasts unless you're dealing with tons of vehicles - because it sounds kinda messy - and just start with a distance check. That should filter out most vehicles that are ineligible so they don't run the rest of the code needlessly. The distance required should scale based on the other car's velocity. You can always change the discovery method later.
Next you want the angle difference between the other car's forward motion and this car's forward motion. Not forward direction, in case the cars are in reverse or something odd like that, but forward motion, (rigidbody.velocity).
The distance and angle difference tells you you're in the zone and should apply drafting. It does kinda make sense that the effect should reduce drag, so I think I'd try that first. How much to reduce drag can be influenced by the other three variables you just finished testing. Use lerps and ranges to find a good value, e.g.
float drag = Mathf.Lerp( 10, 5, percentThroughDraftingEnvelope );
Depending on how detailed you'd like to be, you can add additional complexity to this approach, like raycasts to test for interfering objects, and complex envelope calculations which more firmly establish the physical situation so you can respond with greater accuracy.
Thank you for the response! As of right now gravity is doing the "force." I am making this more of a downhill racer. As of right now, am just doing sphere's, making it super simple. I tried the box collider part way of thinking, but did an additional child object and set its properties to add force. I tried some other things but nothing worked. I think I have to do it through scripting. I also have to consider since drafting is "sharing the air," the front sphere has to also gain some force, or less drag, or whatever way to speed up. Yeah I like the idea about the zone, where it increasingly adds speed to the trailing sphere and adds increasingly (but not as much) to the front sphere. Would be interesting to see if it would work like it would in real life. I was hoping there was some way to just add air to the environment and let it work itself out, but doesn't seem like there is anything built in like that.
Simulating fluid dynamics is for supercomputers! But seriously, it's freakishly complicated to model it "for real". This approach is super duper efficient.
Your situation is very different from what I had pictured. For something this simple, just start with a distance check (visualize a big spherical area). You can turn it into more of a "realistic" envelope with the techniques mentioned, but don't even sweat that until you get the distance check going. The next condition to introduce should be making sure the vehicle in front does not also experience the benefits.
Increasing your rigidbody's "drag" can be thought of as adding thicker and thicker air on a per-object basis. But it does not, as stated, do any fluid-dynamics for you.
Changing the friction of the physics material might get you closer to the results you need? Just play around with these ideas, and I'm sure you'll land on something nice.