Unity Tutorial - How to Make a Drag and Shoot Basketball Game #unity #unitytutorials #gamedev

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in today's video we are going to make this drag and shoot game in unity if you're new here I would love it if you hit that subscribe button below here I set up a simple scene with four walls which have box colliders attached to them let's begin by dragging our ball Sprite into the scene which will create a new ball game object in the scene let's rename it ball next let's add a circle collider to decomponent to it to enable Collision detection and then also add a rigid body to decomponent to it to apply physics to the ball let's set its gravity to three now hit play yes gravity and collision are working fine on the ball but to add real ball physics to the ball first let's create a physics material 2D asset in the assets to create that click on create then select the 2D menu and then select physics material 2D now select the asset that we created and set its friction to 0.1 and bounciness to 0.6 we can change these values later on if we want to now drag this asset to the physics material field of the ball's rigid body component hit play congratulations you just made a ball bounce Now set Collision detection to continue and interpolate mode to interpolate for smoothness now let's create an empty game object and reset its transform and rename it to drag controller now let's create a new c-sharp script and rename it to drag controller then open it let's create two public variables one of typeline renderer and one of type rigid body 2D now drag our script onto the drag controller object the line render component will show us our drag and the rigid body component will store our Ball's rigid body to apply drag Force to it now you can see we have two fields let's drag our ball object's rigid body component to the RB field now let's add a linear endure component to the drag controller object now let's change some of its settings to get reliable results you can follow my settings or experiment with some of its fields to get a better understanding of the line renderer now drag that line renderer to the line field let's create a float variable as the drag limit and set its default value to 3. if we want we can modify it through the inspector next create one more float variable for force and set its default value to 10 create one more variable of type camera and another variable of a type Boo as is dragging now in the start method let's initiate everything once first let's set the line start and end positions as Vector 3.0 and by default our line will be disabled I forgot to set the line position count to 2. it is necessary to set the line renderer's position count before adding or removing points now in the update method first let's see for the mouse button press input and whether dragging or not if the mouse button is pressed but tracking is not set to true then start the drag let's first create a drag start method now in the drag start method the first set is dragging to true and set the line's zero position to the current Mouse position here we are going to use Mouse position multiple times so I created this simple property that simply Returns the mouse position in World space now let's add another if statement if tracking is true that means the player has already started dragging so let's create another method called drag now inside this method let's first take a line start position and the mouse's current positions in two separate variables okay so before doing anything let's set the line second position as the current Mouse position and enable the line in the drag start method now let's add the last if statement here we checking if the mouse is released and dragging is true which means we need to stop dragging now so let's create our last method which is the drag in method here let's set dragging to false and disable the line yup you can see now that our drag is perfectly visualized in the game this can be good to go with but as we want to add a drag limit let's make some changes to the drag method first let's create a vector to store the distance from our start and end positions next let's add a check here if the magnitude of the distance is less than the drag limit then only update the lines and position to the mouse's current position otherwise here we simply created a vector by getting the normalized value of the distance Vector multiplying it with a drag limit and then adding it to the line start position it can be hard to understand at first but it's simply setting the maximum limit position so let's set this position to the line second position oh I think we made some mistakes here we mistakenly updated the first position let's change this index to 1. now you can see that no matter how long I drag it limits the drag to a certain limit we can also change this limit from here to get a suitable result now in the drag in method let's add some Force to the ball first let's copy this code and paste it here and here just replace this part instead of mouse position let's get the line second position because it will be in limit let's create a variable as the final force here simply multiply the force to add variable with the distance variable distance is also a Direction now let's add this Force to the ball and set the Force mode to impulse because we want an immediate Force force is applied to the ball but we want to add the force in the opposite direction here simply add a menu sign before Force so it will add the force and drag's negative Direction now it's done after adding some particle effects and a basket this is how it turned out I appreciate that you are watching this until end if you have any questions please ask them in the comments section and I will respond if you enjoy our videos please subscribe to our channel for more excellent content like this and please tell your friends about it as well see you in the next video stay happy and have a nice day
Info
Channel: Pix and Dev
Views: 5,696
Rating: undefined out of 5
Keywords: Unity tutorial, unity tutorial 2023, game development unity tutorials, unity android game development tutorial for beginners, unity 2d tutorial, how to make 2d game in unity, how to make 2d game in unity tutorial, how to make 2d games in unity for beginners, unity, DragAndShoot, BasketballGame, GameDevelopment, GameProgramming, MobileGameDevelopment, IndieGameDev, GameDesign, UnityEngine, GameDevTutorial, Drag n shoot, drag to shoot, angry bird game in unity
Id: kVH5reaAyII
Channel Id: undefined
Length: 7min 16sec (436 seconds)
Published: Sun Apr 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.