Simple Physics Based Helicopter Controller in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign hello and welcome to a new video and in this video we're going to be doing a helicopter controller because after the last video on the plane controller a lot of people asked me for the helicopter controller one one person asked me but it's super quick to do so I figured why not so just before I get into it there's a quick uh primer on the difference between a helicopter controller and a plane controller I've got a little prop to help me so those of you see in the playing controller will know that the way the plane lifts itself is essentially you get enough forward momentum the air goes under the plane wings and it pushes it upwards and then when you're in the air the faster you go the more upwards Force you get you can pitch to get more upward Force you can tilt you can roll Etc helicopter is slightly different I don't have a helicopter Lego model to help me here so you have to bear with me helicopters just have upward thrust they don't lift by going forwards they just push straight up relative to themselves so you've got the big propeller on the top goes straight up pulls it up like that and then the way they move around is they tilt so that upward Force going that way is now going that way like at a slight angle which pulls it upwards but also forwards and then obviously tilt that way it goes towards tilt this way it goes away Etc that's all we need to do we just need to do that upward fast and then we need to tilt our helicopter in the direction we want to go the little blade at the back we're not going to do in this video because we don't need it that blade is essentially to counteract the rotation of the blade on top so if without that one at the back the helicopter would start spinning around we don't need that because we're just using the physics to move the helicopter around we're not simulating every single particle of air that is going to be interacting with so the helicopter isn't going to start spinning itself we would have to put that force on the helicopter as well as the force that we're imparting to make it move let's go so as you can see we're starting off with more or less a blank project we've got a plane and we've got a helicopter that's it the helicopter is from uni asset store is a free model I will put a link in the description so the first thing we're going to do is head up to our input manager and we want to just rename these to the horizontal and vertical to horizontal it wants to be roll vertical wants to be pitch and then we're just going to copy one of those there we go just saves us having to put all the values in again so I'm going to call this one yeah and the negative button is going to be q and the positive button is going to be e and we don't need Alternatives and we'll leave all the other variables the same back into unity and if we find our helicopter we want to add a rigid body and we want to add a box collider obviously with a proper model you you wouldn't want to with a proper game Sorry you wouldn't want to use a box collider but we're just getting things going here I'll leave the details to you in your own project so the Box collider I'm just going to have roughly covering the mass of the helicopter it doesn't need to be perfect also until you set things up properly you need to have enough collider on the ground to stop it from Just tipping over because that will happen so I'm going to go with that for now it doesn't need to be perfect regarding the mass a little bit of research suggests that an average helicopter a small helicopter would be about 360 kilograms so I'm going to set the master 360 units and then we're going to create a helicopter script I'm just going to drop that onto the helicopter and then we're going to open it up so the first thing we need is a rigid body and then we want a few variables I'm going to serialize field these and make them private because we don't need to access them from outside over them in the inspector the first one is going to be called responsiveness and by default I'm going to set this to 500 this is a value that I came to in testing one that felt about right obviously you play with these and make them feel the way you want it to feel throttle amount is going to equal 25 by default again this is a value that felt decent when I was testing and this is the amount that the throttle will increase and decrease when you increase or decrease the throttle and then finally well not finally but finally for this little section we have the throttle itself and this is just going to be a value between not and 100 that represents the percentage of actual thrust that we're getting I don't want some variables to store our input from the player's button presses or Mouse movements however you decide to control it joysticks Etc so first one's going to be roll and these are going to correspond directly to the keys that we just set in the input manager next one's going to be called pitch and the last one is going to be called yeah so on to our awake function the other thing we need to do in here for now is get a reference to our rigid body component and then in our update all we're going to do is start with is just do handle update which is a function we haven't created yet so let's go create that one to get rid of that error private avoid handle inputs and then in here our first three things we're going to do is get the inputs from the player so that's roll equals input dot get access roll and then we're just going to do the same for the uh the three values pitch and gr so these are case sensitive and I did capitalize them in the input manager so make sure you put those correctly and also this has been typed wrong handle inputs not handle update so the first thing we want to do is handle how much thrust we're currently getting from our engine which is we're I'm going to use the spacebar key for this and I'm just going to use get key because we're not doing an input manager lesson here we're just doing how to make a helicopter fly so keycode.space if you wanted to you could remap the jump button which is currently spacebar by default and then we're going to say throttle plus equals time dot Delta time times throttle amount that's just going to add depending on how much throttle amount we have it's going to add some throttle and then we're going to say else if input dot get key keycode dot left shift and essentially we're doing the opposite and now we're removing throttle so this time it's minus equals and then the last thing we're going to do in this function it's very simple function is clamp those values between not and 100 because remember the throttle is a percentage of the maximum amount of thrust that our helicopter can achieve and that's it for handle inputs so our only function left now to get the helicopter actually up and flying is a fixed update and in the fixed update we're just going to say rigidbody dot add fast transform dot up and the reason we're using transform.orp is because as I mentioned earlier in the video the helicopter moves by essentially always having upward Force relative to itself and then tilting so that upward Force then starts to lean forward a little bit and it starts to go forward and so it's always transformed up rather than Vector 3.0 because Vector three will always Point upwards whereas transform will Point upwards relative to the helicopter that's going to be multiplied by throttle and then we're news Force Mode dot impulse now if we just do a quick test of this hopefully we should be able to make our helicopter fly upwards there we go so as the throttle starts to pick up the helicopter moves off and then to do the rotational values we're just going to say rigidbody dot add talk this time this is same as ad Force but this is applying rotational Force rather than just straightforce and we want transform dot right times pitch times responsiveness and we'll just copy those two more times because I'm lazy and this one wants to be the forward axis and without multiplying this one by row and this one wants to be straight up so this is rotating around the up Axis so it's moving left to right and that's going to be rotated by ya so now we should be able to make our little helicopter move around of course we can see so now we should be able to make a little helicopter twist and turn in the air walk out the door you see someone that you know and they ask you how you are you just have to say that you're fine when you're not really fine but you just can't get into it because they would never understand maybe I want to mess around with the responsiveness values and he's off so the last thing we're going to do is we're going to make the rotor blades rotate essentially and to do that well first off we need a value tell us how fast we're going to rotate them so serialize field private float and this one's going to be rotor speed modifier and it's modifier because it's not the actual speed value it's how much we're modifying the speed value by by default I'm going to set this to 10. it kind of doesn't matter for this because at a certain point you can't tell how fast the blades are spinning this is a purely a visual thing and once the blades get and this this is the same in real life by the way once the blades get over a certain speed you can't really tell if they're spinning faster or slower so I've set it to 10 you can mess around with this value but I'm pretty sure like after a point it doesn't matter we're under reference to the transform itself and it's going to be called sorry private that's going to be called rotis transform and then to apply this we're just going to go down to our update this doesn't need to be in fixed update sorry if I haven't said this already but this all wants to be in fixed update because it's physically and fixed updates is basically for physics so if I put this up here weird things can happen because this doesn't update consistently this has variable intervals between the updates whereas this one as the name suggests is fixed it always has the same amount of time between each update and then in here we're just going to say rotors transform dot rotate and it's going to be vector3.up multiplied by throttle multiplied by rotor speed modifier and then if we go into our inspector and we drag the rotors obviously this doing it this way relies on the rotors being a separate model to the helicopter if you have one rigid object then this won't work I'm just going to drag the rotors into there oh another thing is you may need to change the you may need to change this depending on how your model is set up if you've had to rotate your model 90 degrees to go facing the right way up it's going to change the direction that your rotors are spinning around now for press play we should be able to get a helicopter off the ground this time with spinning rotors hi this is editor Biggs and the more eagle-eyed of you may have noticed that we have a little error down at the bottom here and that is because I have somehow added the script to the helicopter twice so and I've only assigned the rotors to one of those scripts the reason I'm mentioning this in editing is because apparently I forgot to say out loud that I'd removed this extra component when I recorded the video but the more important fact is that this also means that we are applying our forces to the helicopter twice which is also why I kept crashing earlier on because the controls were very sensitive so I just thought I'd point that out thank you bye-bye and one little thing I've just noticed this should be minus I'm rolling the wrong way when I press left I'm rolling right and that's not correct or vice versa okay so I said I'd teach you how to uh make a helicopter flying Unity I didn't say I'd teach you how to fly clearly you need someone else for that but if you want to check out the plane video that I mentioned earlier on you can also see how to get a quick and dirty camera system going as well as rotor sounds like engine sounds for the helicopter that tie into the helicopter's actual throttle which would look like this [Music] thank you one other thing I just want to add that I completely forgot about is the amount of thrust that you can have so right now we are multiplying one by throttle that's fine but I mean it works but it doesn't give us any variability on how powerful a helicopter can be so to fix that let's just add a value here serialize field private float let's call it Max rust and that's going to equal well right now we're using one and that works so let's say five and that should give us a five times bump in amount of the amount of power we have and then this is just going to be enclosed in Brackets and it's going to be Max thrust times throttle so that's it hopefully that video was useful and and short and concise and to the point but you know I've attended to rumble like I'm doing now so the last thing I do is to thank my amazing patreons you all have a special place in my heart but the special special thanks go to uh sugar daddy Mama tear patreon to our Dave maldine red Reed Gabriel white Aaron Clark Mr Drunken Dragon Andrew Hansen and Tim Rea you may notice on the screen that Discord Nitro boosters are also getting a mention on the thank you section as well as anyone who wants to switch over to Kofi anyone who subscribes on Twitch because now I am a twitch streamer as well we'll get some kind of thanks at the end of these videos and that's it for now hope you enjoyed the video and I'm gonna go give my son his toy back
Info
Channel: b3agz
Views: 8,326
Rating: undefined out of 5
Keywords: game development, game development for beginners, game development process, game development tutorial, game development unity, helicopter controller in unity, helicopter controller unity, helicopter in unity, helicopter physics, helicopter physics unity, unity, unity 3d, unity 3d game, unity 3d movement, unity 3d tutorial, unity 3d tutorials for beginners, unity beginner tutorial, unity game tutorial, unity tutorial, unity tutorial for beginners, unity tutorials, unity3d
Id: WzNDI7g6jA4
Channel Id: undefined
Length: 12min 59sec (779 seconds)
Published: Wed Dec 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.