Making Custom Car Physics in Unity (for Very Very Valet)

Toyful Games
8 Jul 202222:48

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

00:00

🚗 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.

05:03

🔍 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.

10:08

🌐 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.

15:12

🚦 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.

20:18

🛠️ 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

A 'couch co-op game' refers to a video game that allows multiple players to play together on the same device or console, typically in the same physical location. In the context of the video, the developers are creating a game where players can work together as valets, which emphasizes teamwork and shared experiences. The script mentions this concept at the beginning to set the stage for the game's collaborative nature.

💡Physics

In the context of game development, 'physics' pertains to the simulation of natural laws and behaviors within a game environment. This includes how objects move, interact, and respond to forces. The script discusses the developers' desire for complete control over the car's physics, indicating their intention to create a game with realistic or stylized physical interactions that contribute to the gameplay experience.

💡Raycast

A 'raycast' is a method used in game development to detect collisions or to find objects in a certain direction from a point. It's like casting a ray into the game world and seeing what it hits. In the script, the developers use raycasts to determine the ground beneath the car's tires, which helps in calculating suspension and friction forces, central to creating a realistic driving experience.

💡Rigidbody

A 'rigidbody' in game development is a component that allows an object to have mass and be affected by forces, such as gravity or collisions. The script mentions using a single rigidbody for the car instead of individual ones for each tire, which simplifies the physics calculations while still allowing for realistic car behavior.

💡Suspension

In the context of the video, 'suspension' refers to the system in a vehicle that allows the wheels to move up and down while maintaining contact with the road. The script describes how the suspension force is calculated using a spring model with an offset and strength, which affects how the car's body moves in response to the terrain.

💡Friction

'Friction' is the force that resists the relative motion of two surfaces in contact. In the video script, friction forces are calculated at the tire location using raycasts to determine the ground interaction. This is crucial for simulating how the tires grip the road, affecting the car's acceleration, braking, and turning.

💡Steering

Steering in the video refers to the mechanism that allows the driver to control the direction of the vehicle. The script explains how a force is calculated in the 'red direction' to prevent tires from slipping sideways, which is essential for accurate steering control in the game.

💡Acceleration

In the script, 'acceleration' is discussed in the context of applying force to the car to increase its speed. The developers use a lookup curve to simulate the car's engine power, which varies based on the car's current speed, providing a more realistic acceleration experience.

💡Braking

'Braking' is the process of slowing down or stopping a vehicle. The script describes how braking forces are calculated similarly to steering forces but in the 'blue direction', effectively removing velocity in the rolling direction to simulate the car's brakes.

💡Torque

Torque, in the context of the video, is a measure of the force that can cause rotation about an axis, which in the case of a car, affects its speed and acceleration. The script mentions a torque curve that represents the car's engine power output based on its speed, allowing for a more nuanced control over the car's performance.

💡Damping

Damping refers to the reduction of oscillations or vibrations in a system, such as a spring. In the video, damping is used in the context of a 'dampened spring' force calculation for the car's suspension, which helps to prevent the car from bouncing indefinitely and provides a more stable and realistic ride.

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

play00:00

We're making a couch co-op game about being  valets, we wanted to make sure we had complete  

play00:04

control of the feel, physics, and the amount  of realism in our cars. Our line of thinking is  

play00:10

that if you have a mechanic in your game that is  central to your game, and you have the skills to  

play00:13

code it yourselves, you should code it yourselves.  That will give you complete control to modify and  

play00:18

adjust it as your game evolves over the course of  development. In this video we're going to go over  

play00:22

step by step all the ways we calculate the forces  required to make custom car physics in Unity. 

play00:28

Alright welcome to our car test facility, let's  take a look at one of the cars from the game.  

play00:32

Prim will give us a little demonstration of  the handling of this high quality sedan here.  

play00:38

We went with a pretty arcade-y, bouncy feel  to the cars. But the techniques we're going  

play00:44

to talk about today can work for simulating  arcade-y all the way to semi-realistic cars. 

play00:50

Let's hop into the little test mobile here  and talk about how these cars work in detail.  

play00:55

The first thing to mention is that we went  with a raycast based approach. Rather than  

play01:01

simulating individual rigidbody for the main  car, an individual rigidbody for each tire,  

play01:08

and connecting them with some kind of physics  joint - there's actually only one rigidbody  

play01:13

going on here. This is what the car looks like  according to the physics system. It's somehow  

play01:17

one rigidbody that seems to be driving around as  though it has tires and works like a car. What's  

play01:25

going on here is that we are creating forces we're  applying to this one rigidbody that make it behave  

play01:31

like a car that has tires attached to it. Here's a look at those forces. You can see  

play01:36

that there's four of them so basically at the  location of each tire we're using raycasts to  

play01:41

find the ground underneath us and then working  out things like suspension and friction forces,  

play01:46

and applying them to the main rigidbody  of the car at the location of each tire.  

play01:50

The net result of these four forces is a  single rigidbody that drives around like a car. 

play01:56

You should be on the right track now, you know  cast some rays, calculate a force for each tire,  

play02:00

and you've got a raycast car. Thanks  and don't forget to like and subscribe! 

play02:05

Okay obviously I'm going to have to go into more  detail because look at these forces they look  

play02:08

*totally* confusing and complex! How are these  being calculated? What kind of crazy math is  

play02:14

going on to make these wild forces that are  constantly changing direction and magnitude...  

play02:19

OMG I don't even know where to get started. The good news is that while this looks really  

play02:23

complicated we can break this problem  down and make it significantly simpler.  

play02:28

The first thing to realize is that the tires  are basically like children of the car. We  

play02:34

created four Transforms that we attached to the  car that represent the location of each wheel.  

play02:40

If we look at one up close, we can think of a  tire as actually having three individual forces  

play02:48

that contribute to its effect on the car. If  I turn a visualization of these we can see  

play02:55

them independently. In the green direction we  have suspension. If Prim bounces up and down on  

play03:02

the car we can see that the force in the green  direction (that is the suspension) is growing  

play03:07

and shrinking depending on how much it needs to  push to keep the car floating above the ground. 

play03:14

In the blue direction you have gas and  brake. You can see the force in the blue  

play03:20

direction is the direction the tire likes  to roll in. If we push a force in the blue  

play03:25

direction we can speed up the car, we can  slow down the car, and we can apply brakes. 

play03:30

Finally you have that red direction  which represents steering, or slipping.  

play03:37

If the tire was moving in the red direction  that would be bad, tires don't like to slip.  

play03:41

What we end up doing is calculating a force in  the red direction that reduces slipping. Says,  

play03:47

"I don't like to be moving in this direction, stop  any movement in this direction. I like to move in  

play03:51

the blue direction, but not in the red direction." We can think about these things separately.  

play03:57

Each tire on this car is just three numbers we  need to calculate. We need to calculate the number  

play04:03

for the green force that says, "What's the  suspension doing?" We need to calculate a  

play04:07

number for the blue force that says, "What are we  doing for rolling, and accelerating, and braking?"  

play04:13

And we need to calculate a number for the red  direction that says, "How do we make sure we're  

play04:18

not slipping and we're actually steering  and rolling in the direction we want?"  

play04:22

That's what we're doing to spend the rest of this  video doing. Breaking down this problem into three  

play04:26

separate numbers that we calculate  independently. Once you calculate  

play04:30

that you get the sum of all those forces  and that is the result for each tire. 

play04:36

Let's start with the green force  we were talking about before,  

play04:38

which is the suspension force. There's a spring  attached to the tires holding up the car up off  

play04:44

the ground. Of course we're talking about a spring  here, we'll need to calculate a spring force. 

play04:52

The first thing to realize about a spring is that  a spring has a rest distance, it has a length that  

play04:56

it likes to be. If you compress the spring it  will push back to the rest distance, or if you  

play05:03

stretch the spring it will pull back to that rest  distance. The first number that's involved in this  

play05:08

calculation is the "offset", which is basically  how far away are we from that rest distance. 

play05:16

Notice this offset needs to be a signed  value. On one side it needs to be negative,  

play05:21

and on the other side it needs to be positive.  This number is telling you which way to go and  

play05:26

how far to go to get back to the rest distance.  If you want to calculate a spring force  

play05:33

that's really all you need! You need  to know the offset and multiply that  

play05:38

by some value that says how strong is this spring. Here's a quick example where we set the strength  

play05:43

to 10. If we push the spring and let go, you can  see this green line (let me scale it down so it's  

play05:50

easier to see on the screen) this is the force  we're calculating. The force is literally just  

play05:55

the current offset times "some number" which  represents the strength of the spring. When  

play06:00

we're pushed all of the way here to an  offset of -0.4 then our force is -4,  

play06:05

and if I stretch the spring the other way we get  a force just as strong in the opposite direction. 

play06:12

You can start playing with values like  this value for strength. What if we  

play06:15

beef up the spring and give  it a much thicker metal coil.  

play06:19

Now you can see the force is a much higher  value that results in a much faster oscillation. 

play06:27

The first step to calculating a spring  force is to calculate the offset  

play06:31

and multiply by the strength of the spring that  gives you a force that you can apply directly.  

play06:38

You might be noticing that this spring seems to  bounce back and forth forever. That's true! If  

play06:42

you have just this much information you get a  spring that will bounce forever. Real shocks on  

play06:48

cars (and other kinds of springs) have what's  called a "damper" that looks something like  

play06:53

this. A plunger of some kind with some fluid and  this thing doesn't want to move. When you try to  

play07:00

push the spring now it has to push the plunger  through that fluid which is resisting motion.  

play07:06

This is what damping is referring to is  trying to slow down the motion of the spring. 

play07:14

We can represent this in code if we know one  other thing: the current velocity of the spring.  

play07:20

Imagine the spring is bouncing back and forth and  we can stop it at any point and understand the  

play07:25

velocity of the object that the spring is attached  to (In this case the tire). If we know that we  

play07:31

can calculate a damping force. A damping force is  basically trying to *stop* any motion. It wants to  

play07:38

resist motion. That's why you take velocity into  account. If I give this thing a little nudge here  

play07:44

we can see it quickly kills any motion. ~wind noises~ 

play07:48

And this is happening with a force that's in  the opposite direction of the velocity. The  

play07:53

velocity is headed to the left and the force  is pushing to the right. The size of the force  

play07:59

is relative to how much velocity we have.  We can see down there the force is negative  

play08:04

velocity times damping. Again damping is just some  number we choose. In this case you can think of  

play08:10

damping as representing how thick is the fluid  that's inside that damper there. The thicker the  

play08:17

fluid the more it's going to resist motion. When we want to calculate a better spring we  

play08:23

combine these two elements. Our force is equal  to offset times the strength (that's trying to  

play08:30

push the spring back to its rest distance) and  we tack on this damping component that says,  

play08:36

"But by the way this spring is resisting motion" When I compress the spring and I let it go you  

play08:41

see we don't oscillate very much. In  fact we come to rest very quickly. 

play08:52

It starts to get a little complicated to look at  all of these arrows and really understand what's  

play08:56

happening. If I slow down a little bit you can  see at the beginning here when the spring was  

play09:00

squished and I let go that we have our offset  is pretty big and that times the strength is  

play09:06

giving us a lot of force saying, "go to the  left" and right now our velocity is pretty  

play09:11

small so it's not doing much. As we speed  up here and head towards the home position,  

play09:16

look at that, our green force  actually flipped directions.  

play09:19

And despite the fact the spring still needs  to go to the left to reach its rest position,  

play09:24

our ultimate force right now is pushing in the  other way. It's trying to slow this thing down. 

play09:29

Slow down, slow down, slow down. That's the damping force  

play09:34

counteracting the regular spring component to  keep this from oscillating over and over again. 

play09:43

What's great about this is once you've  got this formula, it's just four numbers:  

play09:46

(offset * strength) - (velocity * damping). 

play09:50

Two of these numbers are ones you just  decide - the strength and the damping.  

play09:55

You get very different behaviors depending  on what you set these numbers to. Here's an  

play09:58

example of a spring with a strength of 100 and  a damping of 1. This thing is going to bounce  

play10:03

back and forth a lot because the damping is very  small, it has a small effect on the system. This  

play10:08

would be like a spring with a really thick coil  of metal and maybe just air inside the plunger. 

play10:17

If we change this and say the damping is  30 - we took the air out of the plunger and  

play10:20

poured a bunch of honey in there. Now this  spring is really not going to want to move,  

play10:24

so even if I compress it down and let it go  with the same strength coil. UGGGH! It has to  

play10:30

try really hard to push that spring because  it's really resisting and kind of motion.  

play10:36

Maybe somewhere in the middle - take out  that honey and put oil inside. Now you get  

play10:40

something that doesn't resist too much,  lets it get right back to the rest position,  

play10:44

but it doesn't overshoot very much. These are  two numbers that you can tune to get the spring  

play10:48

to behave exactly how you'd like. One thing to  keep in mind these numbers are relative to the  

play10:54

mass involved. Imagine you have a really weak  spring and put it on a car, the car would just  

play10:58

slam down to the ground because it was so heavy.  Similarly here depending on the mass of the  

play11:02

objects you're trying to affect with this spring  you might have to make the strength and damping  

play11:09

bigger or smaller depending on the mass involved. Here we have our final formula for the spring  

play11:17

force! This is what's called a "dampened spring"  force because it has the damper involved.  

play11:22

Basically it's just four numbers. You've got to  work out the offset (how far away are we from our  

play11:27

rest distance), multiply that with the strength  of the spring, subtract the velocity of the thing  

play11:33

the spring is attached to (in the direction  of the spring by the way), times the damping.  

play11:38

And then you get the spring force. This is all  we need to calculate that green force on our car. 

play11:45

Here's a look at the actual code for the  suspension force. As you can see this formula  

play11:49

is basically implemented directly in the code. We  do some setup to work out things like our offset  

play11:55

and our velocity, then we plug those into our  formula right here. Offset times strength minus  

play12:01

velocity times damping. That force is applied  at the position of this particular tire. 

play12:08

Here's a look at a car with just those green  suspension forces implemented. We're able to  

play12:14

keep the car up off the ground now. But if we  bump into the car and send it moving, we've  

play12:20

got shocks but we have absolutely no traction  what-so-ever. Good job, we made a hovercraft! 

play12:31

If our car is driving along here, we can zoom  in on each individual tire and get the velocity  

play12:37

of that individual tire. Just the velocity of  that point on the car's rigidbody. We can break  

play12:43

this up into our blue and red directions. The blue  direction is the direction the tire likes to roll.  

play12:50

The red direction is the direction the tire  doesn't like to slide. We can take the current  

play12:56

velocity force each tire, in this case the orange  arrow. The dot product can break it up into two  

play13:03

separate components. The component that's going  in the rolling direction (blue direction),  

play13:09

and the component that is in the sliding direction  (Red direction). Now we're going to focus our  

play13:14

attention on the red direction because that's  the force we're calculating to produce steering. 

play13:22

This is the current velocity of the  tire in the direction of the red line,  

play13:28

the direction we don't want to be sliding. We  need to calculate some sort of force that says,  

play13:35

"DON'T slide in this red direction!" Ultimately  we don't want any velocity in the red direction  

play13:42

because that velocity means the tire is slipping  sideways. We need to calculate some sort of force  

play13:47

for this. Force is mass times acceleration.  If we want that force we can think about it  

play13:53

in terms of an acceleration. An acceleration is  just a change in velocity. We can ask ourselves,  

play14:01

"What change in velocity would we like to occur?"  Actually that's pretty straightforward, because we  

play14:07

already have this grey line that is the current  velocity in the direction of the red sliding  

play14:13

direction. We don't want any of that velocity to  exist, we don't want to slide sideways. Really  

play14:19

what we want is a change in velocity that is the  opposite - we want to remove all of this velocity.  

play14:25

If we take that current velocity and flip its  direction, that's the change we want to occur. 

play14:33

At this moment when we flip it we could also  scale it down and say, "Well we're okay with  

play14:38

the tire sliding a little bit" so maybe we're only  going to try and remove 50% of the velocity. Or  

play14:44

the car should be sliding a lot right now  (we're on ice) so we'll only remove maybe  

play14:48

10% of the velocity. Basically we're going  to take the current velocity of the tire  

play14:54

in the sliding direction and we're going to flip  it around. That is a change in velocity we want to  

play15:00

have happen. We're going to take that vector and  divide it by some amount of time (a small amount  

play15:05

of time) to say this is the acceleration we want.  We want a change in velocity over time. That value  

play15:12

becomes our force. You can see if we just take the  velocity in the red direction and we completely  

play15:22

negate it and use that as our force, here's a car  example with 100% grip. You can see the tires do  

play15:28

not like to slide. So grippy the car will flip  over depending on things like the center of mass. 

play15:35

Here's an example canceling out only 5% of the  slip velocity. We've got a real slide-y car  

play15:45

(This is front wheel drive car by the way so it's  not going to spin out too much, but it is really  

play15:49

slide-y). Now we've made a drifting game just by  saying we have 5% traction in the red direction. 

play15:57

Here's another example where the front tires have  30% grip, the rear tires have 5% grip, and it's a  

play16:02

rear wheel drive car. You can see this car really  likes to spin out. I'm not even steering right  

play16:07

now and we're still doing donuts. This gives  you a lot of control over the car that you can  

play16:15

think of in simple terms of how much grip do we  want to retain as a percentage for each tire. 

play16:27

In our game we don't just use a fixed value  like this. We use a lookup curve. This is  

play16:34

slightly complicated, but the x-axis you can  think of how much of the tire's velocity is in  

play16:42

the red direction right now. 0 would mean tires  not sliding at all, it's just rolling forward  

play16:47

perfectly. 1 would mean the tire is completely  sliding sideways. We use that number - how much  

play16:57

the tire is sliding as a percentage of its  velocity - and we lookup into these curves  

play17:01

to decide how much traction should the tire have  right now. 1 would mean full traction - let's get  

play17:07

rid of ALL the velocity in the sliding direction.  0 would mean we don't care at all, let it slide.  

play17:14

The shape of the curve basically says if the  tires aren't slipping too much they have a  

play17:20

lot of traction, but once they start sliding too  much they loose tons of traction and become really  

play17:25

slippery. This is based loosely on how real tires  work. Tires have a lot of grip at first because  

play17:32

the rubber is stretching and maintaining contact  with the ground. At some point they start to  

play17:37

slide, and when they start to slide the friction  drops. These are two curves here, a curve for the  

play17:44

front tires and the back tires tuned separately  to give the car the feel I'm looking for.  

play17:48

Ultimately in our game this is how we implemented  the feel we wanted and used a simple curve that  

play17:52

we could draw and test to get the behavior. Here's a look at how that is in code. We have  

play18:00

a little setup here working out velocity in the  steering direction. Then we decide how much grip  

play18:08

we want to maintain. Use that to calculate an  acceleration that becomes a force that we apply  

play18:14

at the location of the tire in the direction of  the red line which is the steering direction. 

play18:20

At this point we have a car with suspension and  also steering forces (or friction forces) so the  

play18:26

tires don't like to slide sideways. If Prim gives  a little push and hops inside we've created a  

play18:33

soapbox derby racer. We have a car with suspension  and steering but no engine and no brakes. 

play18:41

We're down to the last force we need to  calculate which is in this blue direction.  

play18:45

It's comparatively simple because all we really  need to do is apply some force in the blue  

play18:49

direction. Whichever direction the force is, the  tires are going to slowly roll in that direction.  

play18:55

We need to work out what kind of forces to apply  to do things like accelerate and brake the car. 

play19:02

A simple approach for acceleration would be  when the player is pressing the accelerator  

play19:06

we just apply a constant force in  the blue direction. Let's say 100. 

play19:10

GO! 100, 100, 100...right, constant  force equals constant acceleration  

play19:15

so the car is just going to speed up forever. We need something slightly more sophisticated.  

play19:19

In our game we went with this simple animation  curve that represents the power (or the torque)  

play19:25

of the car's virtual engine. The amount of  torque (or forward force in this blue direction)  

play19:36

is based on this lookup curve. The curve is  saying how fast is the car going right now.  

play19:40

Is the car is at a standstill we get 50%  of our total power of our engine. When the  

play19:46

car is going at mid-speed here we have access  to full torque. As we approach our top speed  

play19:53

the torque drops down. The horizontal axis is how  fast is the car going compared to its top speed.  

play20:00

Top speed is just a number we select. Let's say  the top speed of this car is 10 units per second  

play20:05

and right now it's going at 10 units per second  so we have no access to the torque. But now we're  

play20:10

going 0 units per second so we have access to  50% of our torque. That 50% or 100% of our torque  

play20:18

is just a multiplier on some number which  is how strong is the engine which is just a  

play20:23

force value we decided. This is roughly based on  real engines that have less torque down at the low  

play20:30

RPMs and then have a band of their maximum torque  and then the torque drops off as it redlines or  

play20:36

starts spinning really fast as you get up to top  speed. This was enough for us to get somewhat  

play20:41

realistic car that behaved in a way that we could  easily tune and control with just one curve. 

play20:50

That gets the car going, but  what about stopping the car?  

play20:53

Turns out we've already solved this  problem, because stopping the car  

play20:56

is the exact same thing as steering the  car. Remember that the force in the red  

play21:00

direction for steering was basically saying, "Hey  I don't want any velocity in this red direction,  

play21:05

tires don't like to slip, so let's calculate  a force to remove velocity in that direction" 

play21:09

When the brakes are applied we're doing the exact  same thing. We want to remove any velocity in this  

play21:14

rolling direction. We do the exact same math  we do for steering except in the blue direction  

play21:19

this time to produce a braking force. You can  do other things with this like put a maximum  

play21:25

value on it and clamp it so that your brakes  have some sort of maximum force they can apply  

play21:30

that slowly bring the car to a stop. You can also  add things like rolling friction which work very  

play21:34

similarly which is when you're not pressing the  gas we apply a light braking force in the opposite  

play21:40

direction to slow the tires down to a stop. Here's a quick look at the acceleration  

play21:45

code. We're working out the car's speed,  dividing it by the top speed of the car,  

play21:51

using that to lookup into our power curve, and  then applying that force directly in the blue  

play21:56

direction at the location for each tire. There we have it, a fully driving car with  

play22:00

suspension and steering and acceleration and  braking. We can tune this pretty easily by  

play22:05

adjusting a small number of values for things like  the suspension and the grip and the power curve. 

play22:10

Thank you for watching this video, and if  you'd like to support us check out our game  

play22:14

Very Very Valet which is now available on  Nintendo Switch, Steam, and PlayStation 5. 

play22:20

Of course if you like these dev style videos  please give a like and consider subscribing to  

play22:24

our channel and we'll make some more. Thanks again, byyyyyyye!

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Unity GameCar PhysicsArcade HandlingSuspension ForceRaycast MethodGame DevelopmentMechanic SimulationVehicle DynamicsCustom TuningPhysics Engine
¿Necesitas un resumen en inglés?