Unity Spacegame Tutorial - Movement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's tutorial you'll learn how to make a basic rigid body spaceship controller as shown on screen here let's get started right-click in the hierarchy panel and create a new empty name it player spaceship in the inspector panel click on ADD component type in rigid body and enter to add the rigid body to the game object set the mass to 100 the drag to 1 and the angular drag to one turn off use gravity I'm also going to change to the Collision detection to continuous go ahead and set the XYZ position to zero now let's add a visual component drag and drop your model onto the player's spaceship game object if you don't have a model you can follow the rest of the tutorial just using a cube if you'd like to support the channel you could also download this model as well as all of the other models and textures I made for this tutorial in the link in the description underneath our model select every mesh you'd like to contribute to Collision and then go to the inspector and add a mesh Collider don't forget to check convex on your mesh Collider the rigid body on our player's spaceship game object will detect all the mesh colliders and its children objects and use those for its Collision now before we start coding it's a good idea to add something to the scene so that we can actually see that we're moving around make a new material and use Skybox panoramic as the Shader then go to window rendering lighting in the lighting window go to the environment Tab and switch out the Skybox material with the new material that you've just made put some asteroids in there with a rigid body and mesh collider component and then just scatter them all around now it's time to work on the code click on your player spaceship game object go to the inspector and click on ADD component and type in player spaceship hit enter to do a new script and then hit enter or create an ad to add the script just to keep things tidy let's make a new folder called scripts and we'll drag and drop our player spaceship script in there you can double click on the player spaceship and the inspector or in the scripts folder down here to open it in Visual Studio first create a reference for the spaceship rigid body then add in floats for your vertical move horizontal move Mouse input X Mouse input Y and role input the roll input axis doesn't exist by default so we'll have to add it ourselves in unity inside Unity go to edit project settings go to the input manager right click on vertical and click on duplicate array element open it up and change to the name to roll change the negative button to e and the positive button to Q remove the alt negative button and the alt positive button now we'll add floats for Speed mult speed mold angle and speed roll Vault these are good values that should work for the tutorial adding serialized field before a line makes it so that you can access and change it in the inspector now the start method set the cursor lock state to cursor lock mode dot locked this will keep players from moving their mouse outside of the game window and accidentally clicking out of the application Now set spaceship RB to equal get component rigid body this will set it to find the rigid body component on the same game object as our script now in the update method we'll set all of those input floats that we created earlier to input gitaxis to the corresponding axis in the unity input manager now this is where the fun begins create a fixed update method inside our fixed update method we'll reference our spaceship rigid body and then add Force for the vector 3 we're going to do spaceship rigidbody dot transform dot transform Direction and then vector3 dot forward we're going to multiply that by vertical move and then we're going to multiply that by speed mole to a comma and this is where we set the Force Mode we're going to use Force Mode dot velocity change but feel free to play around with the other ones if you'd like if you test the game now you can move forward with w and backward with s feel free to parent your main camera to the player's spaceship by dragging and dropping it onto the player's spaceship game object so that the camera follows you can copy and paste the add Force line we just wrote change the vector 3 from forward to right and change the vertical move input to horizontal move to create strafing now it's time to add rotation to our spaceship we put in spaceship RB dot add torque we're going to set spaceship rb.transform.write for our Vector we're going to multiply that by speed multangle and multiply It Again by Mouse input Y and then personally I like to multiply that by negative one then we'll set the Force mode to Velocity change again copy and paste this and remove the negative one change Mouse input y to Mouse input X and change the transform to up instead now the last thing we have to do is copy and paste our add torque line again this time changes the transform to forward multiplying by the speed roll mult angle and then multiplying by the roll input and there you go your basic rigid body spaceship controller is complete feel free to take a Victory lap around the asteroid belt I hope you enjoyed the video and got something good out of it please like And subscribe and uh I'll see you in the next one where we go over damped camera tracking and switching from cockpit to third person view [Music] thank you
Info
Channel: Nebulaoblivion
Views: 2,343
Rating: undefined out of 5
Keywords: 3d, spaceship, space, space game, spacegame, spaceflight simulator, videogame, game dev, games, game developer, indie game, indie game dev, starfighter, unity, unity3d, madewithunity, tutorial, educational, video game tutorial, unity tutorial, unity C#, coding, video game coding, blender 3d, b3d
Id: hXColAuMx-I
Channel Id: undefined
Length: 6min 36sec (396 seconds)
Published: Thu Dec 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.