How to Make a Pick up System in UNITY With Rigidbodies [C#] [Unity3D]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so here's vinnie all right this person's vinny okay vinny and vinnie just moved into this new scene after purchasing it from the matrix and the problem vinnie has is that he is currently unable to move his boxes into their sorted regions so he wants to move all these blue boxes into this little enclosure for the blue boxes and the red boxes in the red region here and vinnie has hired me to create a script for him that will let him pick these objects up and be able to move them into their specific regions so to start off right now we're first going to need to layer all of these objects here into their own separate pickup layer so we're going to take our like select all these objects here all these boxes and we're going to go to layer here and click on this to collapse it to see all of our layer options and you'll see add layer we're going to click on that and set our first user layer the first one that is available for you and name this pickup and after that we're going to select all of our cubes again and assign their layer to the pickup layer all right and for the next thing we're going to need to create a hand object for vinnie so vinnie doesn't have a hand object yet and we're going to help him with this so he can actually pick the objects up and make sure that in your character controller that your hand object is a child of your main camera object so like like this and call this game object or hand and what you're going to want to do is to be able to see how it would look when the player is holding an object you could put like a temporary game object underneath i'm gonna put a sphere in actually no i'm gonna put a cube in to see what it looks like when vinnie tries to pick up his um his boxes here okay so these are on a scale of 0.75.75 so i'm going to make this temporary cube into 0.75.75.75 and i'm going to move the hand object around while looking at the game view alright so this is what the game view looks like i'm just gonna move this down a bit to the right and like that have it like this yeah like that this looks fine okay all right so when you're happy with um what it where like your hands location based on like its positioning and how it would look like when the player picks up an object you can get rid of the temporary cube and we can get to scripting so in my scripts folder i'm going to create a new class and i'm going to call this our pickup class and we're going to assign it to vinnie here so he can pick up the objects and we're going gonna go and edit it all right so the first thing we're going to do is start creating the main variables we'll need so we can get rid of our start function we don't really need that and up top above our update function we're going to create a serialized field of camera and call this the player camera and we're also going to create a serialized field of a float and call this our pickup range pick up range there and above all of these we're going to create a layer mask a variable a serialized feel of private layer mask and call this our pickup layer there you go and now with these variables we can create the logic for picking up the the cubes so in our update method we're first going to create a raycast and this is what will help us in getting info on the object that we are trying to pick up yeah also uh the objects all these objects have a rigid body attached to them okay so in our update method we're going to create array and we can call this our pickup ray and since it's a first person controller i'm not going to use the mouse to pick up the objects we're just going to use the camera's forward vector so the pickup ray is going to be a new ray of the player camera's transform position and the direction will be the player cameras forward so player camera that transformed off forward with our pickup ray we can now make do our raycast uh physics.raycast here physics.raycast what physics.raycast here there you go and we're going to pass in our pickupray as the first parameter and for the second parameter we're going to do an out raycast hit and cause our hit info so we can get the uh any parameters of the other object like the rigid body and collider which is what we'll mainly need and we're going to put our pickup range as the max distance and the pickup layer as our layer mask there you go now what we're going to do is create two variables underneath our three main ones and it's going to be a rigid body and a collider so this rigid body we can call this our current object rigid body and we're going to create a private collider and call this our current object collider and this is what we'll use to have a reference to uh the object that we currently have selected so we can move it around and keep it at our hands position and rotation so once we hit an object what we're going to do is check if we have a current object being held so um the rigid the current object rigid body and the current object collider will be synchronous um like when they're being assigned so if there's a current object rigid body then there has to be a current object collider as if we click an object then we will assign both of these so uh if we have one of these so if you have a current object rigid body then what we're going to do is uh we're going to drop our current object rigid body and then assign it with the other um with uh the new rigid body that we hit with our raycast but we're going gonna first work on what happens when we click on an object and we do not have a current object rigidbody so we're gonna work on our else statement first right here so this is if we do not have any item so when we don't have any item we simply just assign our variables here so we do current object rigidbody equals hit info from our raycast function dot rigidbody and our current object collider will be hit info dot collider there you go you don't have to do any get component calls the raycast hit struct has a reference to this route to the other objects rigidbody and collider which is very useful all right so there you go and to prevent any weird behaviors like uh for example flying into the sky like uh prop flying in half-life what we can do is for any object that we're picking up we can set the rig their rigid body to uh is kinematic and their collider disabled so setting a rigid body to kinematic will remove that rigid body from any physics calculation so make it exempt from any physics calculations that would normally happen to a non-kinematic rigid body so what we're going to do is after we assign our collider and rigid body we're going to set our rigid body to is kinematic and we're going to disable our collider to prevent any like um any glitching so chronologic collider dot enabled equals false and there you go and we do not want to run this every frame what we're going to do is every time we press a certain key so i'm going to say every time we press the e key so input get key down key code dot e and we can run this logic within this if statement and there you go so now if we go back into unity and we assign our variables here so the pickup layer is the pickup layer right here and we have a reference to our main camera and we set our pickup range to something like 10 a pickup range of 10. and if we hit play i can go to these cubes here press e on them and you'll notice that it is no longer moving around right it's just frozen and i can walk through them so back in our class now we're going to handle with actually holding the object so this so in our update method after we do the check for whenever we press the e key we're going to check if we have a current object rigid body again so if current object rigid body and what we're going to do is set its transform.position to our hand object's position and the rotation too so in our three main variables here we're going to create a reference to our hand so we're going to do a serialized field private transform and call this our hand and with this we can assign our picked up objects transform position and rotation to our hands position and rotation so we can do current object rigidbody dot position equals hand dot position and current object rigidbody.rotation equals our hand dot rotation there so now anytime we press e on an object and we do not have a a current object that we're picking up then we will have an object that will lock onto our hand like this and there's quite some jitter but we'll fix this later and there you go so that's how we can pick up objects now we need to be able to drop and throw objects and whenever we press e on another object we replace our current picked up object with that new object so hit play again go back into our class and now we're going to go work in here so what we want to do is we're going to set our current object rigid body to not kinematic and enable their collider and as soon as that is done with our new hit object we're going to set that to our current object rigidbody and collider so what we're going to do is we can just copy this code here and put it up here but this time we're going to set it's kinematic to false and enable to true and after that what we're going to do is reassign these to our hit info our hit infos collider and rigidbody so we do current object rigidbody equals hit info dot rigidbody and current object collider equals hit info dot collider there and underneath this we can just copy this and put underneath again so what happens is if we have a current object rigid body what we're going to do is set it uh set um set the current one to be able to be calculated within the physics loop and enable its collider and after that we replace it with our newly hit object and with our new hit object we set it to kinematic and we set its collider to disabled there so now if we go back into unity and we go hit play and we press e on let's say a red cube there now i'm holding a red cube if i go to a blue cube here i press e you notice that it replaces it you can see that i keep on dropping the red cube and i drop the blue cube alright so now let's work on dropping objects and throwing them so what we can do is if we press e and we do have a current object rigid body and we did not pick uh hit any object then we can just simply drop it so we can kind of just copy this over move this down here and instead of having it a reference to our hit info what we can do is set this to null null there so basically if we do not hit an object with our raycast then what we're going to do is we're going to set uh just reset our current pickup object and make sure when we do get an object then we hit return so we do not run this after we hit an object and it runs this already now let's say we want to throw an object let's say we want to actually throw something so underneath this what we can do is do an if input.get and i'm just going to use the key q and what we can do is since that these objects here have rigid bodies what we can do is we can apply a force to these uh boxes so it's very easy so what we do is we first set them to we first reset the cubes so we can copy this code down here and after we reset it we're going to apply a force so we do current object rigid body dot add force and the force will be at our camera's forward vector so uh we do player camera.transform.forward copy that move that down here and we do force mode impulse then it will throw the object but first we can create a variable next to our pickup range and call this our throwing force and this will determine how far we will throw the object so we can multiply this by your throwing force and there you go so now if we go back into unity and we go to vinnie here and let's set them give them a throwing force of like five and we hit play we pick up a cube we press e and press q you notice that we throw the object there you go and when we pick it up and we press e we just drop it press q to throw it and we can replace objects and there you go now vinnie can finally sort all of these cubes all right so some boxes were kind of lost in the void of existence uh all right and that is all i have for this tutorial if you learned something new then please like and subscribe and i'll see you in the next video goodbye you
Info
Channel: Rytech
Views: 17,280
Rating: undefined out of 5
Keywords: Pickup System in Unity, Unity Pickup GameObjects Tutorial, How to pick up gameobjects in Unity, Unity pick up, Pick up gameobjects, How to pick up game objects in Unity, How to make a pick up system in Unity, Unity Pick up rigidbodies
Id: Gy5HoXIvL1E
Channel Id: undefined
Length: 15min 57sec (957 seconds)
Published: Thu Jul 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.