Making Custom Car Physics in Unity (for Very Very Valet)
Summary
TLDRIn this video, the creators of a couch co-op valet game discuss their approach to developing custom car physics in Unity. They explain using a single rigidbody with raycast-based forces for tire interaction, detailing the calculation of suspension, steering, acceleration, and braking forces. The video offers insights into tuning car behavior with simple curves and formulas, resulting in a game with arcade-style, bouncy cars that are easy to control and modify.
Takeaways
- 🚗 The game developers are creating a couch co-op game focused on being valets, emphasizing the importance of having control over the game's feel, physics, and realism in car mechanics.
- 🛠️ They have chosen to code the car mechanics themselves to ensure they can modify and adjust the game as it evolves during development.
- 📚 The video provides a step-by-step guide on calculating the forces required for custom car physics in Unity, using a raycast-based approach for simplicity and control.
- 🌟 The game features an arcade-y, bouncy feel for the cars, but the techniques discussed can be adapted for simulating cars ranging from arcade to semi-realistic.
- 🔄 The car's physics are simulated using a single rigidbody with forces applied at the location of each tire, giving the illusion of a car with tires behaving naturally.
- 🔢 The tires are treated as having three individual forces contributing to their effect on the car: suspension (green), gas and brake (blue), and steering or slipping (red).
- 🔄 The suspension force is calculated using a spring model with a rest distance, offset, strength, and damping to simulate the tire's interaction with the ground.
- 🚦 The steering or slipping force is determined by calculating the current velocity in the undesired sliding direction and applying a force to counteract it, affecting the car's traction.
- 🚀 The acceleration force is based on a lookup curve representing the car's engine power or torque, which varies with the car's speed, providing a more realistic driving experience.
- 🏎️ The braking force is similar to the steering force, applying a force in the opposite direction of the tire's rolling to slow down or stop the car.
- 🔧 The game's car physics can be easily tuned by adjusting values for suspension, grip, and the power curve, allowing for fine-tuning of the driving experience.
Q & A
What is the main focus of the video script about?
-The video script focuses on the development of a couch co-op game where players act as valets, with an emphasis on creating custom car physics in Unity.
Why did the developers choose to code the game mechanics themselves?
-The developers chose to code the game mechanics themselves to have complete control over the feel, physics, and realism of the cars in their game, allowing for modifications and adjustments as the game evolves during development.
What approach did the developers use for the car's physics system?
-The developers used a raycast-based approach with a single rigidbody for the car, applying forces at the location of each tire to simulate the behavior of a car with tires.
How many forces are considered for each tire in the car's physics?
-For each tire, there are three individual forces considered: suspension (green direction), gas and brake (blue direction), and steering or slipping (red direction).
What is the purpose of the 'spring force' in the car's suspension system?
-The spring force in the suspension system is used to push or pull the car back to its rest distance, simulating the effect of a spring holding the car up off the ground.
How is the 'offset' in the spring force calculated?
-The 'offset' is calculated as the distance from the spring's rest position, and it is a signed value indicating the direction and magnitude needed to return to the rest distance.
What role does 'damping' play in the car's suspension system?
-Damping is used to slow down the motion of the spring, simulating the effect of a damper in a real car's suspension system, which resists motion and prevents excessive oscillation.
How is the car's traction or grip controlled in the game?
-The car's traction or grip is controlled by calculating a force in the red direction that reduces slipping, based on the tire's current velocity in the sliding direction and a lookup curve that determines traction based on the percentage of sliding velocity.
What is the basis for the car's acceleration and braking forces?
-The car's acceleration and braking forces are based on the direction the tires like to roll (blue direction). Acceleration is controlled by a lookup curve representing the car's engine power or torque, while braking is achieved by applying a force to remove velocity in the rolling direction, similar to steering.
How can the car's behavior be adjusted in the game?
-The car's behavior can be adjusted by tuning various values such as the strength and damping of the spring force, the grip percentage for each tire, and the shape of the lookup curves for traction and engine power.
What platform is the game 'Very Very Valet' available on?
-The game 'Very Very Valet' is available on Nintendo Switch, Steam, and PlayStation 5.
Outlines
🚗 Custom Car Physics Development
This paragraph introduces a video about creating custom car physics for a couch co-op valet game. The developers opted to code the car's mechanics themselves for full control over the game's feel and realism. They discuss the importance of a mechanic-centric approach and provide an overview of the step-by-step process for calculating the forces necessary for realistic car behavior in Unity. The video promises a detailed explanation of the custom car physics, starting with a demonstration of the game's car handling and the decision to use an arcade-style, bouncy feel. The approach uses a single rigidbody for the car, with forces applied to simulate tire behavior, achieved through raycast calculations for suspension and friction at each tire's location.
🔍 Breaking Down Car Physics Forces
The second paragraph delves into the complexities of calculating the forces for car physics. It starts by simplifying the problem into manageable parts, focusing on the tire as a 'child' of the car with three contributing forces: suspension (green), rolling/accelerating/braking (blue), and steering/slipping (red). The paragraph explains the concept of calculating spring force for the suspension, including the importance of offset and spring strength, and introduces the idea of damping to prevent endless oscillation. Damping force is calculated based on the current velocity of the tire, and the paragraph concludes with the formula for a dampened spring force, which is a combination of spring force and damping force.
🌐 Tire Friction and Traction Control
This paragraph discusses the calculation of forces that prevent tires from sliding sideways, which is crucial for steering and traction. It explains the process of breaking down the tire's velocity into rolling and sliding components and focuses on eliminating sliding velocity to prevent tire slip. The concept of force as mass times acceleration is introduced to calculate the necessary acceleration to change the sliding velocity. The paragraph explores different levels of grip through examples, such as 100% grip and 5% slip, and mentions the use of lookup curves to determine tire traction based on the percentage of sliding velocity. The implementation of these concepts in the game allows for fine-tuning of the car's handling characteristics.
🚦 Engine Power and Braking Mechanics
The fourth paragraph addresses the implementation of engine power and braking in the car physics. It starts with a simple approach to acceleration, applying a constant force, and then moves on to a more sophisticated method using an animation curve to represent engine torque based on the car's speed. The curve determines the force applied in the rolling direction, simulating the behavior of a real engine with varying torque at different speeds. The paragraph also explains how braking is achieved using the same principles as steering, by applying a force to remove velocity in the rolling direction. It mentions the potential for adding maximum brake force limits and rolling friction to enhance realism.
🛠️ Fine-Tuning the Car's Driving Dynamics
The final paragraph wraps up the video by summarizing the car's driving dynamics, including suspension, steering, acceleration, and braking. It emphasizes the ease of tuning the car's behavior by adjusting a few key values, such as suspension settings, grip levels, and the power curve. The video concludes with a call to action, inviting viewers to support the developers by checking out their game, 'Very Very Valet,' available on various platforms, and encouraging likes and subscriptions for more developer-style content.
Mindmap
Keywords
💡Couch Co-op Game
💡Physics
💡Raycast
💡Rigidbody
💡Suspension
💡Friction
💡Steering
💡Acceleration
💡Braking
💡Torque
💡Damping
Highlights
The game developers prioritized creating a mechanic for car physics that they could fully control and modify throughout the development process.
A step-by-step guide on calculating custom car physics in Unity is provided in the video.
The developers opted for an arcade-y, bouncy feel for the cars, with techniques applicable to a range of realism levels.
A raycast-based approach was chosen for the car's physics, simplifying the simulation to a single rigidbody.
Forces are applied to the main rigidbody at each tire's location to simulate the behavior of a car with tires.
The tires are treated as children of the car, with three individual forces contributing to their effect on the car.
Suspension force is calculated using a spring model with rest distance, offset, and strength.
Damping force is introduced to simulate the effect of a shock absorber, considering the spring's velocity.
The formula for a dampened spring force is presented, combining spring force and damping force.
The car's suspension force is implemented in code, applying the calculated force at each tire's position.
Traction force is calculated to prevent sliding by removing velocity in the undesired direction.
A lookup curve is used to determine tire grip based on the percentage of sliding velocity.
The car's acceleration is controlled by a power curve representing the engine's torque relative to speed.
Braking force is calculated similarly to steering force, by removing velocity in the rolling direction.
The video demonstrates how to implement acceleration and braking forces in code for the car's tires.
The final car physics include suspension, steering, acceleration, and braking, all tunable through a few key values.
The game 'Very Very Valet' is available on multiple platforms, and the video concludes with an invitation to support the developers.
Transcripts
We're making a couch co-op game about being valets, we wanted to make sure we had complete
control of the feel, physics, and the amount of realism in our cars. Our line of thinking is
that if you have a mechanic in your game that is central to your game, and you have the skills to
code it yourselves, you should code it yourselves. That will give you complete control to modify and
adjust it as your game evolves over the course of development. In this video we're going to go over
step by step all the ways we calculate the forces required to make custom car physics in Unity.
Alright welcome to our car test facility, let's take a look at one of the cars from the game.
Prim will give us a little demonstration of the handling of this high quality sedan here.
We went with a pretty arcade-y, bouncy feel to the cars. But the techniques we're going
to talk about today can work for simulating arcade-y all the way to semi-realistic cars.
Let's hop into the little test mobile here and talk about how these cars work in detail.
The first thing to mention is that we went with a raycast based approach. Rather than
simulating individual rigidbody for the main car, an individual rigidbody for each tire,
and connecting them with some kind of physics joint - there's actually only one rigidbody
going on here. This is what the car looks like according to the physics system. It's somehow
one rigidbody that seems to be driving around as though it has tires and works like a car. What's
going on here is that we are creating forces we're applying to this one rigidbody that make it behave
like a car that has tires attached to it. Here's a look at those forces. You can see
that there's four of them so basically at the location of each tire we're using raycasts to
find the ground underneath us and then working out things like suspension and friction forces,
and applying them to the main rigidbody of the car at the location of each tire.
The net result of these four forces is a single rigidbody that drives around like a car.
You should be on the right track now, you know cast some rays, calculate a force for each tire,
and you've got a raycast car. Thanks and don't forget to like and subscribe!
Okay obviously I'm going to have to go into more detail because look at these forces they look
*totally* confusing and complex! How are these being calculated? What kind of crazy math is
going on to make these wild forces that are constantly changing direction and magnitude...
OMG I don't even know where to get started. The good news is that while this looks really
complicated we can break this problem down and make it significantly simpler.
The first thing to realize is that the tires are basically like children of the car. We
created four Transforms that we attached to the car that represent the location of each wheel.
If we look at one up close, we can think of a tire as actually having three individual forces
that contribute to its effect on the car. If I turn a visualization of these we can see
them independently. In the green direction we have suspension. If Prim bounces up and down on
the car we can see that the force in the green direction (that is the suspension) is growing
and shrinking depending on how much it needs to push to keep the car floating above the ground.
In the blue direction you have gas and brake. You can see the force in the blue
direction is the direction the tire likes to roll in. If we push a force in the blue
direction we can speed up the car, we can slow down the car, and we can apply brakes.
Finally you have that red direction which represents steering, or slipping.
If the tire was moving in the red direction that would be bad, tires don't like to slip.
What we end up doing is calculating a force in the red direction that reduces slipping. Says,
"I don't like to be moving in this direction, stop any movement in this direction. I like to move in
the blue direction, but not in the red direction." We can think about these things separately.
Each tire on this car is just three numbers we need to calculate. We need to calculate the number
for the green force that says, "What's the suspension doing?" We need to calculate a
number for the blue force that says, "What are we doing for rolling, and accelerating, and braking?"
And we need to calculate a number for the red direction that says, "How do we make sure we're
not slipping and we're actually steering and rolling in the direction we want?"
That's what we're doing to spend the rest of this video doing. Breaking down this problem into three
separate numbers that we calculate independently. Once you calculate
that you get the sum of all those forces and that is the result for each tire.
Let's start with the green force we were talking about before,
which is the suspension force. There's a spring attached to the tires holding up the car up off
the ground. Of course we're talking about a spring here, we'll need to calculate a spring force.
The first thing to realize about a spring is that a spring has a rest distance, it has a length that
it likes to be. If you compress the spring it will push back to the rest distance, or if you
stretch the spring it will pull back to that rest distance. The first number that's involved in this
calculation is the "offset", which is basically how far away are we from that rest distance.
Notice this offset needs to be a signed value. On one side it needs to be negative,
and on the other side it needs to be positive. This number is telling you which way to go and
how far to go to get back to the rest distance. If you want to calculate a spring force
that's really all you need! You need to know the offset and multiply that
by some value that says how strong is this spring. Here's a quick example where we set the strength
to 10. If we push the spring and let go, you can see this green line (let me scale it down so it's
easier to see on the screen) this is the force we're calculating. The force is literally just
the current offset times "some number" which represents the strength of the spring. When
we're pushed all of the way here to an offset of -0.4 then our force is -4,
and if I stretch the spring the other way we get a force just as strong in the opposite direction.
You can start playing with values like this value for strength. What if we
beef up the spring and give it a much thicker metal coil.
Now you can see the force is a much higher value that results in a much faster oscillation.
The first step to calculating a spring force is to calculate the offset
and multiply by the strength of the spring that gives you a force that you can apply directly.
You might be noticing that this spring seems to bounce back and forth forever. That's true! If
you have just this much information you get a spring that will bounce forever. Real shocks on
cars (and other kinds of springs) have what's called a "damper" that looks something like
this. A plunger of some kind with some fluid and this thing doesn't want to move. When you try to
push the spring now it has to push the plunger through that fluid which is resisting motion.
This is what damping is referring to is trying to slow down the motion of the spring.
We can represent this in code if we know one other thing: the current velocity of the spring.
Imagine the spring is bouncing back and forth and we can stop it at any point and understand the
velocity of the object that the spring is attached to (In this case the tire). If we know that we
can calculate a damping force. A damping force is basically trying to *stop* any motion. It wants to
resist motion. That's why you take velocity into account. If I give this thing a little nudge here
we can see it quickly kills any motion. ~wind noises~
And this is happening with a force that's in the opposite direction of the velocity. The
velocity is headed to the left and the force is pushing to the right. The size of the force
is relative to how much velocity we have. We can see down there the force is negative
velocity times damping. Again damping is just some number we choose. In this case you can think of
damping as representing how thick is the fluid that's inside that damper there. The thicker the
fluid the more it's going to resist motion. When we want to calculate a better spring we
combine these two elements. Our force is equal to offset times the strength (that's trying to
push the spring back to its rest distance) and we tack on this damping component that says,
"But by the way this spring is resisting motion" When I compress the spring and I let it go you
see we don't oscillate very much. In fact we come to rest very quickly.
It starts to get a little complicated to look at all of these arrows and really understand what's
happening. If I slow down a little bit you can see at the beginning here when the spring was
squished and I let go that we have our offset is pretty big and that times the strength is
giving us a lot of force saying, "go to the left" and right now our velocity is pretty
small so it's not doing much. As we speed up here and head towards the home position,
look at that, our green force actually flipped directions.
And despite the fact the spring still needs to go to the left to reach its rest position,
our ultimate force right now is pushing in the other way. It's trying to slow this thing down.
Slow down, slow down, slow down. That's the damping force
counteracting the regular spring component to keep this from oscillating over and over again.
What's great about this is once you've got this formula, it's just four numbers:
(offset * strength) - (velocity * damping).
Two of these numbers are ones you just decide - the strength and the damping.
You get very different behaviors depending on what you set these numbers to. Here's an
example of a spring with a strength of 100 and a damping of 1. This thing is going to bounce
back and forth a lot because the damping is very small, it has a small effect on the system. This
would be like a spring with a really thick coil of metal and maybe just air inside the plunger.
If we change this and say the damping is 30 - we took the air out of the plunger and
poured a bunch of honey in there. Now this spring is really not going to want to move,
so even if I compress it down and let it go with the same strength coil. UGGGH! It has to
try really hard to push that spring because it's really resisting and kind of motion.
Maybe somewhere in the middle - take out that honey and put oil inside. Now you get
something that doesn't resist too much, lets it get right back to the rest position,
but it doesn't overshoot very much. These are two numbers that you can tune to get the spring
to behave exactly how you'd like. One thing to keep in mind these numbers are relative to the
mass involved. Imagine you have a really weak spring and put it on a car, the car would just
slam down to the ground because it was so heavy. Similarly here depending on the mass of the
objects you're trying to affect with this spring you might have to make the strength and damping
bigger or smaller depending on the mass involved. Here we have our final formula for the spring
force! This is what's called a "dampened spring" force because it has the damper involved.
Basically it's just four numbers. You've got to work out the offset (how far away are we from our
rest distance), multiply that with the strength of the spring, subtract the velocity of the thing
the spring is attached to (in the direction of the spring by the way), times the damping.
And then you get the spring force. This is all we need to calculate that green force on our car.
Here's a look at the actual code for the suspension force. As you can see this formula
is basically implemented directly in the code. We do some setup to work out things like our offset
and our velocity, then we plug those into our formula right here. Offset times strength minus
velocity times damping. That force is applied at the position of this particular tire.
Here's a look at a car with just those green suspension forces implemented. We're able to
keep the car up off the ground now. But if we bump into the car and send it moving, we've
got shocks but we have absolutely no traction what-so-ever. Good job, we made a hovercraft!
If our car is driving along here, we can zoom in on each individual tire and get the velocity
of that individual tire. Just the velocity of that point on the car's rigidbody. We can break
this up into our blue and red directions. The blue direction is the direction the tire likes to roll.
The red direction is the direction the tire doesn't like to slide. We can take the current
velocity force each tire, in this case the orange arrow. The dot product can break it up into two
separate components. The component that's going in the rolling direction (blue direction),
and the component that is in the sliding direction (Red direction). Now we're going to focus our
attention on the red direction because that's the force we're calculating to produce steering.
This is the current velocity of the tire in the direction of the red line,
the direction we don't want to be sliding. We need to calculate some sort of force that says,
"DON'T slide in this red direction!" Ultimately we don't want any velocity in the red direction
because that velocity means the tire is slipping sideways. We need to calculate some sort of force
for this. Force is mass times acceleration. If we want that force we can think about it
in terms of an acceleration. An acceleration is just a change in velocity. We can ask ourselves,
"What change in velocity would we like to occur?" Actually that's pretty straightforward, because we
already have this grey line that is the current velocity in the direction of the red sliding
direction. We don't want any of that velocity to exist, we don't want to slide sideways. Really
what we want is a change in velocity that is the opposite - we want to remove all of this velocity.
If we take that current velocity and flip its direction, that's the change we want to occur.
At this moment when we flip it we could also scale it down and say, "Well we're okay with
the tire sliding a little bit" so maybe we're only going to try and remove 50% of the velocity. Or
the car should be sliding a lot right now (we're on ice) so we'll only remove maybe
10% of the velocity. Basically we're going to take the current velocity of the tire
in the sliding direction and we're going to flip it around. That is a change in velocity we want to
have happen. We're going to take that vector and divide it by some amount of time (a small amount
of time) to say this is the acceleration we want. We want a change in velocity over time. That value
becomes our force. You can see if we just take the velocity in the red direction and we completely
negate it and use that as our force, here's a car example with 100% grip. You can see the tires do
not like to slide. So grippy the car will flip over depending on things like the center of mass.
Here's an example canceling out only 5% of the slip velocity. We've got a real slide-y car
(This is front wheel drive car by the way so it's not going to spin out too much, but it is really
slide-y). Now we've made a drifting game just by saying we have 5% traction in the red direction.
Here's another example where the front tires have 30% grip, the rear tires have 5% grip, and it's a
rear wheel drive car. You can see this car really likes to spin out. I'm not even steering right
now and we're still doing donuts. This gives you a lot of control over the car that you can
think of in simple terms of how much grip do we want to retain as a percentage for each tire.
In our game we don't just use a fixed value like this. We use a lookup curve. This is
slightly complicated, but the x-axis you can think of how much of the tire's velocity is in
the red direction right now. 0 would mean tires not sliding at all, it's just rolling forward
perfectly. 1 would mean the tire is completely sliding sideways. We use that number - how much
the tire is sliding as a percentage of its velocity - and we lookup into these curves
to decide how much traction should the tire have right now. 1 would mean full traction - let's get
rid of ALL the velocity in the sliding direction. 0 would mean we don't care at all, let it slide.
The shape of the curve basically says if the tires aren't slipping too much they have a
lot of traction, but once they start sliding too much they loose tons of traction and become really
slippery. This is based loosely on how real tires work. Tires have a lot of grip at first because
the rubber is stretching and maintaining contact with the ground. At some point they start to
slide, and when they start to slide the friction drops. These are two curves here, a curve for the
front tires and the back tires tuned separately to give the car the feel I'm looking for.
Ultimately in our game this is how we implemented the feel we wanted and used a simple curve that
we could draw and test to get the behavior. Here's a look at how that is in code. We have
a little setup here working out velocity in the steering direction. Then we decide how much grip
we want to maintain. Use that to calculate an acceleration that becomes a force that we apply
at the location of the tire in the direction of the red line which is the steering direction.
At this point we have a car with suspension and also steering forces (or friction forces) so the
tires don't like to slide sideways. If Prim gives a little push and hops inside we've created a
soapbox derby racer. We have a car with suspension and steering but no engine and no brakes.
We're down to the last force we need to calculate which is in this blue direction.
It's comparatively simple because all we really need to do is apply some force in the blue
direction. Whichever direction the force is, the tires are going to slowly roll in that direction.
We need to work out what kind of forces to apply to do things like accelerate and brake the car.
A simple approach for acceleration would be when the player is pressing the accelerator
we just apply a constant force in the blue direction. Let's say 100.
GO! 100, 100, 100...right, constant force equals constant acceleration
so the car is just going to speed up forever. We need something slightly more sophisticated.
In our game we went with this simple animation curve that represents the power (or the torque)
of the car's virtual engine. The amount of torque (or forward force in this blue direction)
is based on this lookup curve. The curve is saying how fast is the car going right now.
Is the car is at a standstill we get 50% of our total power of our engine. When the
car is going at mid-speed here we have access to full torque. As we approach our top speed
the torque drops down. The horizontal axis is how fast is the car going compared to its top speed.
Top speed is just a number we select. Let's say the top speed of this car is 10 units per second
and right now it's going at 10 units per second so we have no access to the torque. But now we're
going 0 units per second so we have access to 50% of our torque. That 50% or 100% of our torque
is just a multiplier on some number which is how strong is the engine which is just a
force value we decided. This is roughly based on real engines that have less torque down at the low
RPMs and then have a band of their maximum torque and then the torque drops off as it redlines or
starts spinning really fast as you get up to top speed. This was enough for us to get somewhat
realistic car that behaved in a way that we could easily tune and control with just one curve.
That gets the car going, but what about stopping the car?
Turns out we've already solved this problem, because stopping the car
is the exact same thing as steering the car. Remember that the force in the red
direction for steering was basically saying, "Hey I don't want any velocity in this red direction,
tires don't like to slip, so let's calculate a force to remove velocity in that direction"
When the brakes are applied we're doing the exact same thing. We want to remove any velocity in this
rolling direction. We do the exact same math we do for steering except in the blue direction
this time to produce a braking force. You can do other things with this like put a maximum
value on it and clamp it so that your brakes have some sort of maximum force they can apply
that slowly bring the car to a stop. You can also add things like rolling friction which work very
similarly which is when you're not pressing the gas we apply a light braking force in the opposite
direction to slow the tires down to a stop. Here's a quick look at the acceleration
code. We're working out the car's speed, dividing it by the top speed of the car,
using that to lookup into our power curve, and then applying that force directly in the blue
direction at the location for each tire. There we have it, a fully driving car with
suspension and steering and acceleration and braking. We can tune this pretty easily by
adjusting a small number of values for things like the suspension and the grip and the power curve.
Thank you for watching this video, and if you'd like to support us check out our game
Very Very Valet which is now available on Nintendo Switch, Steam, and PlayStation 5.
Of course if you like these dev style videos please give a like and consider subscribing to
our channel and we'll make some more. Thanks again, byyyyyyye!
Ver Más Videos Relacionados
How to MASTER BAKU on F1 23! | Track Guide
Space Marine 2 - Review After 100%
TL;DW - If you're not in control, you're not going to improve.
Front Wheel Drive in Forza Horizon 5 | Tuning Quickie
Hukum Newton Tentang Gerak (Hukum 1 Newton, Hukum 2 Newton, dan Hukum 3 Newton)
GRÖßENWAHN bei der FELGENWAHL! - Lotus Omega & Ford Fiesta 1100X - Wir VERMESSEN & RECHNEN!
5.0 / 5 (0 votes)