How to Pick Up + Hold Objects in Unity (FPS)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial i will show you how to pick up objects throw them and even rotate them around with no clipping or buggy player interaction real aaa quality also all the code in this video will be available on github and a version with loads of comments explaining absolutely everything so link in the description first let's get into the setup i'm assuming you already have your first person controller and an object you want to pick up the pickup script is going to be on the camera of your character so let's create that and first declare some variables first a public reference to our player and a public transform hold pass next back to the scene create an empty game object called hold position under the camera this will be the position our object will be held at drag in front of the camera to the distance you want the object to be held in front of you on our pickup script drag in the player and the whole position then let's go to our object we want to pick up in this case is this cube create a new tag called can pick up this should be applied to all objects that you want to be able to pick up assign this to your object the last bit of setup we have to do is create a new layer and a new camera why are we doing this well this method involves making the rigid body on the object kinematic and this allows for the object to clip through walls like this which is something you might have encountered if you followed other tutorials before this we don't want this to happen so what we have to do is make a new camera and layer in order to always render the object in front of the walls or other objects when we are carrying it so make a new layer i will call it hold layer then make a new camera under your current camera i'll rename it to pick up camera on the pickup camera change clear flags to depth only and the cutting max to only be the hold layer change the clipping planes to a smaller number like 0.01 and change the depth to 1. next back on the main camera under curling mask deselect the pickup layer so what we just did is make the main camera not render anything on the pickup layer and make the pickup camera only render on the pickup layer so the object we want to pick up will always be on top of anything else we are rendering this technically doesn't stop the object from going through walls though but that problem is dealt with in the code now onto the code i'm not a fan of the old i type and then you type along with me tutorial format i think it's boring and it just takes too long so that's why i suggest you just read through the code and copy paste it from github where i've added a ton of comments explaining absolutely everything so that even a dog could do it if you're in 2020 your dog ain't typing your papers you ain't living right i'm going to explain the rotation function though the rotation is pretty project specific as it interacts with your mouse lock script or character controller this is because the rotation is done by using the mouse axes to apply rotation on the object but you probably don't want the player to be looking around while doing this since it is project dependent depending on how you input your mouse movement it's gonna be different for everyone the way i would set it up is reference the script here define the script in the start function by getting component from the player or whatever the script's on either disable that script or change some values in the rotate function and then change them back just below that's basically it for the tutorial i made this because i couldn't find a non-buggy picking up tutorial so if this helps you and you want to see more vids like this video and subscribe and if you have any suggestions for another tutorial please comment it down below see ya
Info
Channel: JonDevTutorials
Views: 16,241
Rating: undefined out of 5
Keywords:
Id: pPcYr3tL3Sc
Channel Id: undefined
Length: 3min 42sec (222 seconds)
Published: Tue Apr 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.