- Home /
Help. Newbie trying to pick up, put down, and throw two kinds of boxes in a 2D space. Thought it would be easy
So, basically as the title says, I'm trying to pick up and put down a Small and Big Box like how it's shown in this little basic animation
I've had a little look around and I'm not entirely sure what I need to do. Some seem like I'll get close to what I want but I'm not sure what I should follow. I tried one and got the box to disappear but that's it. I tried my own knowledge to see if I could just wing it but that failed miserably. I just need someone to steer me in the right direction cause I'm coming up empty and confused
Also, from what I could tell, being able to walk in front of the boxes and also being able to hop onto the Big Box from the front wouldn't really be possible in conjunction with being able to pick them up. Could someone confirm that before I spend 30 minutes or so trying to make it work?
Thanks in advance for any help you can give!
Answer by AllTheGoodNamesWereTaken · Jun 17, 2020 at 03:51 PM
Here is what I would do. I would first make all the boxes have one way collision detection on top facing upwards (shown here in red).
When walking near the box and pressing E Input.GetKeyDown("e") first disable the collision detection and gravity of that box. Then make the box a child of the player and move the box using local position so the player looks like they are carrying it.
To throw the box when the user presses Q Input.GetKeyDown("q"), make it so it is no longer a child of the player, enable the collision detection and gravity. Then add force to the Rigidbody2D component of that box in the direction the player is facing.
If you need additional help just ask. Thanks
Sorry for the late response, but when I have a box with a one way collision and a rigidbody it falls through the floor. Admittedly I'm using the Platform Effector as a stand in for now but I get the feeling that'll need to be changed for this. The only fix that I immediately can think of is having a 2nd very tiny box collider at the bottom to serve as a catch for the box but that still wouldn't really help as I would probably still be able to kick the box
Oh my you are right! I didn't think of the collision detection between the box and the ground, sorry.
I would create 2 layers. A Player Layer and a Box Layer (the ground can stay on the default layer for now). I would set the player to be on the Player Layer. I would make sure any boxes have a box collider and set the box to be on the Box Layer. Then I would make an (empty) child of the box just to put the one way collision detection on top of the box. $$anonymous$$ake sure that child is set to the default layer.
In the Unity menu bar, go to Edit > Project Settings, then select the [Physics 2D] category to open the Physics 2D window. In the Layer Collision $$anonymous$$atrix there should be a box where Player Layer and Box Layer meet. $$anonymous$$ake sure that box is unchecked. Let me know if you need more help. Cheers!
Bruh, that was such a simple fix I feel kinda dumb for not looking harder. Also, I just spent the past hour or so writing this code and I get the feeling none of it's right. It doesn't come up with an error but it doesn't work either. I've borked up somewhere
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 Answers
Add audio. 1 Answer
Don't Flip Child, Only Parent. 0 Answers