FULL PICK UP & DROP SYSTEM for WEAPONS or ITEMS || Unity3d Tutorial
Вставка
- Опубліковано 20 сер 2024
- FULL PICK UP & DROP SYSTEM || Unity3d Tutorial:
Since I just started my series on how to shoot with projectiles in Unity, I thought it would be cool to share how to make a full pick up and drop system :D
Don't forget to subscribe if you enjoyed the video, and if you have any questions just write a comment, I'll answer as many as I can! :D
Links:
➤ DOWNLOAD the SCRIPT: www.mediafire.c...
➤ DOWNLOAD the PROJECT (in the free-code-dave channel): / discord
➤ DON'T CLICK: bit.ly/2VcrDZt
If you want here's the series about the GUN SYSTEM USED in the video:
➤ • SHOOTING with BULLETS ...
🏹 Get Access to my RANGED COMBAT LAB! 🏹
➤ Trailer: • RANGED COMBAT LAB - Un...
➡️ Get Access to my full MOVEMENT LAB! ➡️
➤ Trailer: • MOVEMENT LAB - Unity P...
Other interesting videos :D
➤ Full WALL RUN tutorial: • Full WALL RUN TUTORIAL...
➤ My FIRST YEAR of game development: • My FIRST YEAR of UNITY...
Support me on Patreon:
➤ / davegamedevelopment
Music used:
➤ Elevate - LiQWYD: • Elevate - LiQWYD (No C...
➤ Night Out - LiQWYD: • Night Out - LiQWYD (No...
#UnityTutorial #PickUpWeapon #Unity3d
If you need help with any of the code shown, just visit the "coding-help" channel on my discord server :D
Also you can find many more tutorials on my channel in the "Tutorials!" playlist - I really hope they help you on your further GameDev journey!
And in case you're interested in getting access to my full RANGED COMBAT LAB, with which you can create pretty much any ranged ability/weapon you can think of, check out the trailer:
🏹 RcLab Trailer: ua-cam.com/video/j2YplilHjCA/v-deo.html
Thanks so much for watching, hope this tutorial has helped you!
This guy is such a good youtuber. He creates full, easy to understand tutorials and responds to almost every comments even if it's off topic. 100% liked and subbed :)
Thank you so much! :D
@@davegamedevelopment i dont know why the Pickup stuff didnt work
the gun is droped but The pick up does not work
but thx u helped us so much 😁😁
I almost cried as it worked, i spent over 10h on it
I feel you man... Sometimes coding stuff takes wayy too long :D
Dave / GameDevelopment I have a question. Will it work if I just use Right click in the part where it says E or or what does the code need to say if I want right click pick up ?
Input.GetMouseButtonDown(1)
@Domantuso_gaming mate can you just say fuck and shit without censoring it
@@developer2 lmao
This man deserves more support his tutorials are amazing! I’m writing this comment to show all the support I can!
This Guy Is A Such A Good Person, He Gives The Codes And Understands We Easily
Thanks a lot!
Set Interpolate to :None
if your object is not moving as your view camera.
Thanks a lot
Thanks, i tried the last month to do a good pick up and drop script cause there is no one on youtube. You just got one new active member :) TY
Glad it helped you! Thanks for watching :D
@@davegamedevelopment thanks for doing it
top 10% of Unity tutorials right here, and I've sure as hell watched pretty much all of them
Coming back to these tutorials after learning c# and they are so much easier to understand!
same.
my game just became 1 step closer 2 done. Thank you so much for this tutorial!
That's nice to hear, glad I could help!
Hi I've been working on top down 2d game and cant really find a good tutorial for pickup and drop weapons so I have no choice but to use this tutorial and after editing the whole script to work with 2d I've manage to make it work so awesome thanks for this tutorial helpful enough!
Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupGun : MonoBehaviour
{
public Gun gunScript;
public Rigidbody rb;
public BoxCollider coll;
public Transform player, gunContainer, fpsCam;
public float pickUpRange;
public float dropForwardForce, dropUpwardForce;
public bool equipped;
public static bool slotFull;
private void Start()
{
if (!equipped)
{
gunScript.enabled = false;
rb.isKinematic = false;
coll.isTrigger = false;
}
if (equipped)
{
gunScript.enabled = true;
rb.isKinematic = true;
coll.isTrigger = true;
slotFull = true;
}
}
private void Update()
{
Vector3 distaceToPlayer = player.position - transform.position;
if (!equipped && distaceToPlayer.magnitude
thx
you are a life saver
thx
thank you from Russia😁
Thanks
:D
You my good sir is a legend among this community, insane tutorial💪🏻
im so glad i can find a tutorial youtuber like you bc most of the other youtubers are just like do this do that then you will have this. but u explain what everything means wich is very helpful for beginners like me.
super well done man! Even though it's almost a 2 year old video it helped me a lot.
Glad you like it!
Thank you, I hope you helped many people, for some reason, this broke my entire game having to completely restart. Nothing against you, probably something i did wrong, but i hope you can help other people
Oh sorry, I don't think it was your fault, sounds more like Unity's problem, normally a simple script shouldn't break your whole game... :(
But glad your not angry, if you want you can still download the project file and check out how everything is set up there :D
@@davegamedevelopment All good, just got a different tutorial for pick up and drop thanks for replying
Absolutely amazing, thank you so much, my player can now pick up a flashlight in my horror game! 😁
Mannn this is such an amazing tutorial! everything worked perfectly at one go! I wish you all the best with your channel!
are you using his movement system and if so where does the guncontainer go to?
@@itzdimi7569 I'm not using his movement system...but my gun container is the child of my camera
Very nice
Used it in my.personal project.
Working well
Enjoying playing with the script.
Thanks a lot, glad you like it! :D
To everyone who has the problem with this line:" public ProjectileGun gunScript;" u have to use instead of ProjectileGun the name of the gun script u have to shoot and stuff. So Get the name from the gun script and put it instead of "ProjectielGun". for example it should look like this :public Gun gunScript;
Thanks bro that really helps me out
Thankyou so much Dave, works very well making my game so much better!
Glad I could help!
@@davegamedevelopment Sorry to ask, but would you know how to make dani's grid material, not the prototype grids one but the new one that he usesin his videos
Dave you truly are amazing so far you are the only unity tutorial creator that shares his code for free and also the only one who has code that works first try love your tutorials they help me real good and it's easy to understand thank you so much keep it up
Thanks you so much i keep finding this tutorial
No problem :D Congrats for first comment btw :D
@@davegamedevelopment you are an amazing guy (;
@@imzary Thanks :D
@@davegamedevelopment np :D
Gotta say man seriously good content unlike any other youtuber! keep it up!
You are the best! I hope you keep making these tutorials!!!!
this guy is as good as Brackeys (R.I.P Brackeys YT Channel)
Hey Dave, so I realized that when you pick up the gun, it just teleports to you. But when you throw it, it smoothly reacts to gravity and makes it feel that you're actually throwing the gun. I want to also feel that I am picking up the gun, not teleporting it to my hand. Anyways, nice video. Keep it Up!
For this you need to learn a bit about the Vector3.Lerp and Quaternion.Slerp functions, then it should move smoothly from one point to the other :D
But for some reason it didn't really work when I tried out
@@davegamedevelopment slerp
Hi! I know it is bit late but anyway. I think you already solved it but for any chance if anyone is looking for this it is a bit simple but tricky.
For this instead of setting local position in pick up you will do it in FixedUpdate(). (Note: I would love to share the script however I made many changes to the original version for my preferences)
1) Just add two new parameters :
float pickUpTime;
bool pickingUp;
2) Then you will chance script a bit. You will remove the part where you set your gun position. Instead you will add two lines :
pickingUp = true;
Invoke(nameof(PickUpFinished), pickUpTime);
3) Make a new method :
void PickUpFinished()
{
pickingUp = false;
}
4) Last step paste in these lines into FixedUpdate() and that is all :
if (pickingUp && currentWeapon != null)
{
Vector3 zero = Vector3.zero;
currentWeapon.transform.localRotation = Quaternion.Slerp(currentWeapon.transform.localRotation, Quaternion.Euler(Vector3.zero), Time.deltaTime * pickUpTime * 10f);
currentWeapon.transform.localPosition = Vector3.SmoothDamp(currentWeapon.transform.localPosition, Vector3.zero, ref zero, 1f / (pickUpTime * 10f));
}
Note : These variables there are not that perfect. I haven't completed it yet put it does the job for now.
I hope that is what you need. Good luck with your game!
@@zeaglee3432 Thank you it helped me a lot, it's all working fine but when I change the pickUpTime on the inspector nothing changes so I'm wondering why? is it supposed to be act like that or not?
@@zeaglee3432 says currentWeapon doesnt exist
cool bro
it helped me a lot with making a game in unity
Thanks a lot! Glad you like it :D
For those whose guns are falling through the floor when you drop it, try changing the size of the box collider and make it so it wont clip the floor when you throw it
there u go mate ur new sub and a follower...i like ur content keep posting more..all the best
Thank you so much! :D
This tutorial helped me a lot, thank u so much now i've eradicated what i was having trouble with. i love u mate, i mean i love u as a friend really... oh no... it is coming back
Amazing content and explanations!
Thanks!
for anyone whose gun is still shooting even tho its unequipped, put the code that you put in hte start function in the update function. so it doesnt just exacute that from the start.
thank you so much
@@JrMinalga np
First tutorial with pick up and drop combined. Thxx for that. Without this i would die
Haha, glad they could help you! :D
My god, thank you soooooo much for adding the script download!!!!! :)))
No problem :D
Hi, I know this video was posted 2 years ago, but hope you can help. I followed the tutorial and got done, but, when I click E it will go to what I set it to go (Item Container) to but then it just floats around and doesn't exactly stay with the Item Container when I look around. How do I fix this?
Great tutorial, Dave has the best unity tutorials
It's perfect!
Currently working on a simple FPS like Hotline Miami, but with Max Payne's slow motion (or SUPERHOT with generic slow-mo?). Ability to grab and throw guns was just what I needed.
Thanks! Glad I could help!
For anyone who is experiencing their weapons floating up on pickup try making a separate game object called "gun stash" or something and put all of your weapons inside there. it fixed the issue for me.
Thnx buddy. It helped me a lot
Bruh, Lifesaver
This video is much better than the others cuz now he's showing hierarchy
I set the rigidbdy to extrapolate and continous dynamic but it still bugs?
Been waiting for this one
Well that's odd. I followed your tutorial and encountered a weird bug. When using extrapolate, my item would stay in place wherever i go but would still rotate. however when i switched it to none. it fixed it.
Not sure if this has something to do with the new unity version but yeah. If anyone else encounters the same bug, that's how i fixed it. Not sure if it's a permanent fix, but it gets the job done.
I've been trying to fix it for so long now but to no avail. Setting the interpolation it to none do actually fix it but the physics of the gun wouldn't be smooth when reacting to gravity or other rigidbodies. If anyone has a fix to it please tell me
@@mhausage3454 hey bro did you ever find anything out? i just had to change interpolation to none and im hoping you found a fix for the rough physics?
I need some help. When i pick up the gun the gun does not follow the player. It just stay in the air. But if i press left click the gun is shoting.
same
When i start the game the guns at the ground just slied away and if i pick them up i cant shoot. Can someone pls help me?
You are just awesome dude !!! Thanks !!!
Thanks a lot! Glad you liked it! :D
Dani aproves about the player object
what happens with me is my gun dont stay in my hand it starts going forward when i go backward and when i go backward it gos forward and ive tried turning off all rotations on the rigid body but it didnt work all I know is that everytime i enable the rigid body it stays in one place plsss help
the only thing that works is the throw machinic which gets thrown from a far distance
Did anyone else get a bunch of errors when trying this?
So a slight problem. I imported a model and now it just shrinks it after I pick it up, I tried with a model I made and one I downloaded.
make sure all parents are scaled at 1 1 1. The character camera weapon and container. if you need to change the size do it from the item in the project window/ go to the model option and change scale factor to what you need.
you explained it that good i even did it in 2D game.
Great tutorial, just one thing. After adding this, the shooting just broke. The bullets now fly backward and not out of my barrel. Any idea why this is happening?
Thank you so much! This worked for me! You've earned my sub!
the weapons does unequip and requip however when it unequips it teleports to 0,0 anyone know why?
edit: it teleports because my weapon is a melee weapon with animations so once you unparent it, the weapon teleports to the location of the animation. to fix this simply disable the animation on Drop() and enable again on pickup()
bro ur a life saver like u got no idea i had this issue in January 2021 and i didnt know how to fix it so i switched to unreal engine and made a whole game but now im back to unity tryna make a game but can u give me the script for the animation part like how to deactivate it when u drop the gun and activated it when u pick it up
@@Saud.alhadhod yeah for sure it's like a line ilk go find it
@@Saud.alhadhod
full code is this the main thing is saving your animaot component to a variable (mines called anim) and adding
anim.enabled=false/true on the drop and pickup functions
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
public weapon gunScript;
public Rigidbody rb;
public BoxCollider coll;
public Transform player, gunContainer, fpsCam;
public Animator anim;
public float pickUpRange;
public float dropForwardForce, dropUpwardForce;
public bool equipped;
public static bool slotFull;
private void Start()
{
if (!equipped)
{
anim.enabled=false;
gunScript.enabled = false;
rb.isKinematic = false;
coll.isTrigger = false;
}
if (equipped)
{
anim.enabled=true;
gunScript.enabled = true;
rb.isKinematic = true;
coll.isTrigger = true;
slotFull = true;
}
}
private void Update()
{
Vector3 distaceToPlayer = player.position - transform.position;
if (!equipped && distaceToPlayer.magnitude
@@stevejobs1609 thankk u sooo much bro i appreciate it❤️🔥
@steve jobs omg tysm! i was so confused
if I drop mine it goes under the map itself any ideas?
Did you solve this please?
thanks, after 3 days i finally found this video
Why he sounds a bit like Brackeys
what if he was brackeys all along
Thanks man ur great help for who just start learning XD
How do I add guns? if I add guns the just move with me. pls help
One sentence for this video: YOUR AN ABSOLUTE LEGEND btw can you also make a vid of how to pick up grenade and throw it thank you
Haha, thanks so much man! :D
Also if you watch my 2 part series on Shooting with Bullets you should be able to make this grenade yourself :D (Just use an invisible gun to throw it :D)
@@gaminganimator-qp2ir some people don’t know how to script yet
go to throw the the 'gun' in my case just a cube, and it works, try again a second time and it plumets straight down, no momentum. Any help appreciated
im a bit late, but when i made the gun a child of the container, the gun would rotate and aim upwards, i checked the transform and it was the exact same. Also, when i drop my gun, the gun would teleport upwards to a specific place and just float there, i checked through my script and it was the exact same as yours.
I had the same problem, have you solved this yet please?
I've the same problem and I still can't fix it. I think he will never respond again :(
@@danielmay1846I think I’ve solved it, if your gun has an animation, when the idle animation is played, it will teleport to the root position (usually 0 0). So, you have to edit the gun drop script so it turns off the animation when it drops, and reenables when picked up. Remember to put the disable animation at the top so it disables animations first before dropping.
@@playerplus5917 I think I’ve solved it, if your gun has an animation, when the idle animation is played, it will teleport to the root position (usually 0 0). So, you have to edit the gun drop script so it turns off the animation when it drops, and reenables when picked up. Remember to put the disable animation at the top so it disables animations first before dropping.
@@_lonely_3124 I've a grappling gun, but I don't really know *how* to turn off the animations. I tried a lot, but it still teleports to the root position
Hi, great video!
I have a problem that when i put the script on more than one gun then i press E on any one gun the other gun comes to me please help.
You need to set the "Equipped" bool correctly before you start the game :D
Dave / GameDevelopment K, thanks a lot
my gun when i shoot getting smaller how to repair?
BEST PICKUP SYSTEM EVER THANK YOU SO MUCH
i a shame you have only 1k subs. more people should know about you (also i like your vids they help me a lot so thanks 🙂)
Thanks a lot! Glad they helped you :D
Everything is working good but whenever i drop the gun it goes back to 0 0 0 because i have my gun container in my fps cam, anyway i can fix this?
Did you solve this please??
Thankyou so much for helping
I'm having an issue with the shooting. I want to start the game without holding the gun which works fine. The gun isn't equipped and I can't shoot it. I pick it up and I'm able to shoot it and it's marked as equipped. However, after dropping it again, I'm still able to shoot it. Is there a way I can fix this? Thanks
Did you find a solution?
i dont get the projectilegun part what exactly is that
Really good tutorial!
Hi great video btw but I have a problem
You typed projectile gun which means I could also type the name of my weapon if I'm correct but it couldn't be found and asked if I'm missing a directive or assembly reference
I need help please reply 🥺
You need to take the exact name of your gun script (The one that makes your gun shoot :D)
@@davegamedevelopment yup I found out yesterday lol thanks for the reply
@@davegamedevelopment im confused, i try to rename it but it says that there is already a script with the same name
@@ezrafarwell393 You probably used a name that's already used by Unity (like System, Input etc.) :D
Try changing it :D
Dave / GameDevelopment thank you so much!
The type or namespace name 'ProjectileGun' could not be found (are you missing a using directive or an assembly reference?) Help me fix it!
How is your gun script called? It either needs to be exactly "ProjectileGun" or you need to Change the variable type to whatever the name of your gun script is :D
Thank you, I'll try that
My gun keeps falling through the floor when i press q and throw, and i cant see it when i uncheck equipped. help?
Add it a box collider
good shit, brackeys level!
He even sounds like him
where do i change the gun script name someone help
Cool tutorial keep it up 😃😃👍👍
Thanks a lot! :D
:)
Love Your Channel! Perfect!
HELP!! when i dont start with the gun equipped it doesn't work. when i pick it up, the gun keeps its position of where it was on the ground instead of teleporting back to vector3.zero. the gun is still parented when you pick it up though, but just isn't in the right position
when i try to have multiple guns, if I don't parent to the camera, I cant pick it up. but if I do, it follows the camera even when on the ground
Try making a storage system, where every Gun object you have is parented to GunContainer, but each has a key. When you press that key, it disables the current Gun Object and activates the Gun Object paired to that key.
omg thanks so much for this, you helped me alot!
Great Tutorial I learned so much and everything is working for me except one thing. I can't see other weapons I picked up and maybe you know why it's like this. But keep on that good work.
Edit: I actually fixed it so GREAT tutorial
Thanks a lot! :D And glad you could fix it :D
i have the same problem, could you tell me how to fix this?
Great content!
Thanks! :D
How do I make it so I can have a secondary primary and a third option? I’m not using this script for guns it’s for items
I loved this, its exaclty what i needed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Glad you liked it, thanks for watching! :D
Dave / GameDevelopment thanks for hearting my comment
What if I have multiple gun scripts?
So I'm trying to add more than one gun but every time he picks up a new one it will fly in some direction
Brooooo is anyone else having this problem, i press e and it picks up the weapon, it looks fine but as soon as i move the character starts glitching, not moving in the right direction and most of the time it just phases through the map, ive played with all the rigidbody controls, tried many varients which caused many glitches and the least glitchiest is the extrapolate and continuous speculative which is where i just fall through the map. the worst part is that the arrow keys get mixed for some reason, so up is left, down is making him fly, right makes him go backwards, etc etc etc etc. its just weird and idk if its the tutorial or my movement script, which i also worked on and played with to see if it was the problem, and i put my my movement and camera look parts of the script in normal update instead of fixed update... yeah obviously it didnt work just did the same thing so any ideas on how to fix this? should i get a new movement script? does anyone have a better fps tutorial???
Hey when i use my pickup script, it works fine as long as the gun is in my camera and set to equipped. Any other weapons just don’t transform to the player when i equip them and im not sure why. i don’t know what’s wrong so if i figure it out i’ll update it here in the comments in case i get no replies
do you already find the sollution? cause i have the same problem
Amazing content, thank u❤️
Glad you like it! :D
GunSystem Script is not working.
It is showing camshake is missing
HELP!
ok i need help the script works for the most part i can pick up and drop my guns and they start and stop working when i pick up and drop them but im having this issue where when they are in my hand and i move they move like 2x more than i do so if i move lets say 5feet over to my right the gun will move 10feet (maybe not exactly 2x but you get my point) and they jitter in a way when i move i have everything identical to what is in the video and it still wont work idk what the issue is please if anyone had this issue and was able to resolve it could you help me out
U solved it now? I have the exact same thing
my pickup class needs a return method
Thank you so much this works very well you earned yourself a new sub.
But i wanted to say that if i have a weapon without a gun script (for example a knife) it will say “Object reference not set to an instance of an object” & if i copy the script and take off the gun script reference it will think its another script so it will let me pick up both weapons at the same time.
When i pickup my other weapon it flies away and then when i drop it and pickup something else, it disappears
Love this tutorial really excellent and well explained. How would I apply it to Photon for a multiplayer game? I have been trying to get it to work but it seems to delete the object after I drop it meaning I cannot pick it up again.
AMAZING!!! I THOUGHT IT WOULDNT WORK BECAUSE I HAVE A WHOLE DIFFERENT MOVEMENT AND CAMERA SYSTEM BUT IT DID, YOURE DOPE
hey rob question when setting the weapon container script (gun container but its weapons since using a sword) do i still need to position the container to the side of the fps camera or is that already done through code? like
transform.SetParent(WeaponHolder);
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.Euler(Vector3.zero);
transform.localScale = Vector3.one;
@@dcry1003 place the weapon position where you want it to be while equipped, move the container to where you want it, make sure it isnt equipped. then reset the transform on the weapon itself and it should work
@@linkdev3916 thanks rob got it working now!👍
@@dcry1003 im glad i could help!
you are legend my friend!
Keep up monster 💜
HELP!!!!!!!!!!!!! I cant understand why whenever I drop my weapon it goes to the center of the world. pls help me if you know how to fix.
Are you using Vector3.zero anywhere in your code?
@@davegamedevelopment found the problem! I had two off the same script on the weapon and it made some bugs thank you so much for the help and the amazing videos!!!
only one problem the animator is overriding the position of the weapon any idea how to stop it without stopping the animator?