Arcade Style Car Controller || Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

When is part 2?

👍︎︎ 1 👤︎︎ u/Hesso921 📅︎︎ Apr 26 2021 🗫︎ replies
Captions
in this video i'll be making a physics-based arcade style car controller used in a fascinating concept it works by having the player control a sphere that the car then follows around the scene we'll start by creating a new project using the built-in render pipeline we'll do this by clicking the 3d icon and we'll give this a name once it's loaded we'll start with the default layout and an empty scene we'll rename the scene to something other than sample scene for our main camera we'll set the clearflex to solid color and like always i'm going to be using a dark gray for the directional light we'll get rid of the yellow hue and we'll reduce the intensity of something like 0.75 we'll quickly fix this game window by setting its aspect ratio to 16 by nine because i'm running 1920 by 1080 from our resolution now lastly let's set up the environment by giving the car something to drive on we'll create a new empty game object called environment making sure that its transform position is completely zeroed out and as a child will create a plane and call it ground we'll set its scale to 50 by 50. now let's create a materials folder inside our project window and we'll create a gray material to drop onto our ground object for this project i'm going to need a car model and i chose to use this free asset on the unity asset store called arcade free racing car and i also need an environment piece to test the car and i'm going with the low poly road pack both the links will be in the description so before we set up our car let's go to our package manager and import these assets to begin creating our car we'll go to the hierarchy and create a new game object we'll make sure that its transform position and zeroed out as normal and we'll name this object car we'll navigate to the arcade free racing car and go into the prefabs folder now i'm gonna use the red car so we'll just grab this guy and pull him straight into the car game object and we'll check the transform making sure it's zeroed out now we'll focus in on the model i notice the rear wheels are out of position to fix this we'll start by unpacking the prefab and make this its own game object we'll expand it out and find the rear wheels we'll position these and do the same for the front wheels now we'll rename it to car model since it's only the graphics and our logic will be on the parent game object and then we'll collapse it back up now we'll make the sphere that we'll actually be controlling we'll go to the root card game object and create a new sphere we'll call this guy motor sphere since we'll be using physics forces to actually move this thing we'll need a rigidbody component and we'll set its drag to four now we'll go into isometric view to see what we're working with and we'll need to align the sphere to the top of the ground by holding ctrl while i drag it'll snap in increments and i can get it lined up now let's exit out of isometric view and get ready for the script i notice i put my materials folder inside the scene folder so let's drag that out and into the main asset folder now inside our asset folder we're going to create another folder called scripts now inside the folder let's create our car script and we'll name it car controller give it a second to compile and then we'll drag that onto our car game object we'll double click the script and let it open we'll delete the comments right fast we want to get the player's input then we want to move the sphere and then we want the card to follow the sphere we're going to work backwards for a minute because with the setup a few problems arise so first we'll set our cars transformed to the sphere's transform and to do that we need a reference to the sphere's rigid body so we'll say public rigid body and we'll call it sphere rb now on the update we'll set the car's transform position to the sphere rb's transform position we can do that by saying transform.position equals sphere rb dot transform dot position [Music] so let's control s to save and then we'll head back into unity to play test so before we play let's go to the car and make sure to drag motor sphere into sphere rb well that's not good is it with a better look nope still ain't good so let's investigate we can see our sphere is here and our car is here we can see they aren't lined up at all but in fact if we switch the car to pivot we'll see that its pivot is at the very bottom so here's the car set up and its pivot point is right here when we press play the pivot snaps to the sphere but here lies the problem the sphere is also parented to the car so now the sphere is trying to be pulled back to its original position this causes the sphere to bounce back and forth between these two positions and since it's a physics object affected by gravity at some point it falls low enough that when it gets snapped to the position it was as a child object it clips through the floor and the car disappears into oblivion so let's fix this problem we want the car's pivot point and the sphere's pivot point to be in the same location with the pivot mode still selected i'm going to grab the car and move it up to where i think i remember the center of the sphere being holding control will allow us to snap and make this a bit easier now this also moved our sphere so let's select it and put it back right above the ground so if we switch between the car and the sphere we can see now that the pivots are aligned if we check the transforms of these objects you'll see that the car is at 0.5 but the sphere says zero and that's because that's the sphere's local position to the car if we pull it out from the car for a second we'll see that both transforms are indeed 0.5 so let's parent that back under the car and now all we need to do is select the car model and drag it back in place so after all that we can hit play to test now you'll see that the car doesn't bounce itself to death now we can go ahead and set up our input for our player we can do this by going into our update and we're going to type move input equals get input axis raw and we want to use vertical as the axis now we'll go into our declarations and we'll set the move input to be a private float and then we'll need a public variable called forward speed the multiplier input so we can control how fast the car will travel so back in our update we'll take our move input and say times equal forward speed now we'll be using this to move our sphere and since it's a physics object we'll need to create a fixed update function and in here we'll say sphere rb dot add force and we'll say the force we want is transform dot forward times move input and as an extra argument we'll use force mode dot acceleration since we're dealing with vehicle type physics we'll save and go back into unity and test let's go to our forward speed variable and we'll set that to be something like 200 we'll see that it works but we're going way too fast so now what's the problem so in our script we're actually moving the sphere and having the car follow so let's say we're moving forward first the sphere moves forward but now when the car follows the sphere the sphere moves even farther forward and this puts the car in sort of a position where it's always playing catch up and hence getting faster and faster so this is an easy fix and the start function of our script will just say sphere rb.transform.parent is equal to null and this simply detaches the sphere from the car so if we go back into unity and test again now you'll see that we're traveling at a more reasonable speed now let's make our car move slower in reverse and give the player the ability to turn the car because that's important so let's make a doppelganger of forward speed called you guessed it reverse speed and we'll jump back in our update now we could write out this long if statement that says something like if move input is greater than zero then we're going to move forward and this would be move input times equal forward speed else you're going backwards so we do move input times equal reverse speed but there's a cleaner way to do this up here we still won't move input times equal but we'll erase this and instead put move input is greater than zero question mark and then we'll type out forward speed colon reverse speed this is known as the ternary conditional operator so the condition is move input is greater than zero and if it returns true we'll use forward speed and if it returns false we'll use reverse speed now we'll go back into unity to test and we'll set this new variable to half the speed of forward so that'll be 100 and we'll hit play to test and that looks like it's working well so let's go ahead and get our turning set up so we'll set up a new float called turn input and a new float called turn speed we'll go into the update and grab our turn input the same way we grab our move input only this time we'll be using the horizontal axis so we move the car forwards and backwards with the sphere and the fixed update but doing that with rotation would be a bit tricky since spheres tend to act a little crazy the other method we have is to move the actual car if we rotate the car but move forward and backwards with a sphere it should work but what if you're thinking well the sphere isn't a child of the car after the game starts so it won't rotate with the car anymore well you'd be correct but if you check out where we rotate the sphere this transform dot forward is actually the cars transformed out forward so we're still good by doing it this method so since we're dealing with the car again and it's not a physics object we'll do this in the update we'll use transform.rotate and then we only want to affect the y coordinate of the car so we'll use zero for the x we're gonna use new rotation for the y and zero for the z this is going to be relative to space dot world now directly above this will declare this new rotation as a float and it's going to be turn input times turn speed and to make it frame independent we'll multiply it by time dot delta time so let's save and test we'll set the turn speed to 150 now you'll see that we got turning and it instantly feels like a nice arcade style car it's even got a nice little drift there's two problems though let's address the one that's obvious the car turns while we're sitting still now go back into the script we could set up a boolean and check whether there's input or not and only have the car turn while the bull is true but there's a clever way to go about this the vertical axis is returning either negative one zero or one if we multiply our rotation by this axis then we automatically cancel out if there's no input and even better when we're pressing forward it gives the same value as it was before because it's multiplying by one and if we press backwards it multiplies by negative one and it flips the rotation for us this results in the car steering naturally in both directions now we have steering so let's address the problem that's a little harder to catch so to demonstrate i've set up a couple of trail renderers on the car now when i hit play you see something happened to these trails you see that that my friend is jitter and it's almost inevitable because of the way we have this set up cars being affected during the update and the sphere during the fixed update the problem is that the update can run many more times a second than the fixed update in this example we'll just assume it runs twice as often and you can still visualize the problem so say we're already moving forward the car is traveling forward because the sphere is traveling forward now we're going to turn left our first update is run and our car starts to turn left the next update is run and our car continues to turn even more now finally the fixed update runs and finally our sphere decides to move the same way as the car it completely jumps over a big section of rotation and we see this as jitter luckily there's this thing called interpolation if we go back to our scene and select the rigid body of our sphere we can select interpolate and this will help smooth this out and with it turned on you can see a big difference now if your race course is on a completely flat ground then congratulations you're finished but if you're going to have hills and slopes or you're getting a little crazier with ramps we're gonna need to do a few more things now let's dig into this other asset we downloaded and we're gonna pick out a ramp let's slap this in the scene and test okay so the car doesn't follow the slope of the ramp nor does it respect gravity okay so it totally disrespects gravity for that matter so in our script we're going to fix rotation of car and fix gravity of car and the way we're going to do this is with a raycast so in our update we're going to do a ground check using a raycast so first we'll create a raycast hit and we'll call it hit now we'll go back up to the top and we're going to need a boolean to check if we're grounded and we're going to call this is car grounded now back in the update we can say is car grounded equals now we can say physics.raycast and our origin is going to be transform.position we're going to raycast downwards with transform dot up but we're gonna use negative and without assigning a direction this is one unit by default we're gonna pass out our hit and we need to make sure that it's only detecting our ground so we don't get a false is grounded by colliding with our car or our sphere we'll call the layer mask ground layer and back up to the top we'll make a new public layer mask called ground layer so back in unity we'll select the ground plane and we'll need to create a new layer and call this the exact same thing we did in the script we'll select the ground plane and assign the ground layer and then also to the ramp that we just made so back in the script below the raycast ground check we'll add something to rotate our car to make it parallel to the ground surface is on we'll do this by saying transform dot rotation equals quaternion and we'll use from to rotation which does exactly what it sounds like we'll rotate our transform.uprotation to the raycasthit.normal and the normal is just the direction the geometry is facing we'll multiply that by our transform dot rotation now in the fixed update we'll say if our car is grounded and you can move and then we'll say else if you're not grounded we want to add some extra gravity and we'll do this by saying sphere rb dot add force and then the force is transform dot up times negative and we'll say 9.8 f for starters so let's save and test and we can't move so let's make sure that our raycast is actually shooting down by one meter so we're going to add in a distance argument into the raycast and we're just going to say 1f so i was just about to test again and i noticed that on my car i didn't have my ground layer assigned in the script so go ahead and do that so now when we test we can see that our rotation is fixed but there's something going on with the gravity still we'll go back into the script and we're going to pump this value up to something like 30. okay that's better but it's not quite perfect so i can't remember if i said anything but the sphere's drag was way too low to begin with so i cranked it up to four but now that's way too much for the gravity to take effect so we need to go back into our script and we need to change the drag based on whether we're in the air or not so back up to the top we're going to make two public floats one's going to be called air drag and one's going to be called ground drag so let's go down to the bottom of our update and underneath where we rotate our car we're gonna say if his car is grounded sphere rb dot drag equals ground drag else sphere rb dot drag equals air drag now on our car on the car controller on the air drag we're going to use a value like point one and for the ground drag we're gonna keep that at four now let's save and test hopefully for the last time and that looks pretty good to me now you have the basics to an arcade car controller drifting included you can get the exact feel you want by messing around with a combination of the sphere's rigid body settings and the other settings that we coded ourselves and you can add tons of other things tire marks headlights exhaust plumes having the tires rotate etc etc have fun go wild until next time spawn camp out
Info
Channel: SpawnCampGames
Views: 33,197
Rating: undefined out of 5
Keywords: IndieGameDev, GameDev, Unity3d, Devlog, MadeWithUnity, GameDevelopment, Tutorials, Devtorials, Blender, Photoshop, Probuilder, SpawnCampGames, Car Controller, Arcade Car Controller, Arcade Car
Id: TBIYSksI10k
Channel Id: undefined
Length: 21min 20sec (1280 seconds)
Published: Thu Nov 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.