How to pick up Objects
Hello Everyone!
Today I am doing a how-to on our games pick up feature.
Summary
We make a box collider to detect the object we want to pick up, add a child game object to the player so the object has a position to move to that is based on the player, we have to reference the object we want to pick up through code, we have to move the object, and then we have to make it so the player can drop the item.
Now lets get started.
In the scene
The player object has 2 box colliders, one for physically running into objects and another for colliding with objects you can pick up,we make the latter collider a trigger so it doesn't hit other objects.
We also have to make sure any object we want to pick up have either the SmallObject or the LargeObject layer, and now we can get to the nitty gritty and code.
Code
At the top our Object Holding script we have all our variable (I put them in a region so when you open the script you aren't bombarded).
Near the top we have all our object reference, we create an empty game object that will hold the information of the game object we pick up, the placeToHold game object will let us reference the transform of the place to hold object which will be the place we want to object we will be holding to move to, the transform of the playerCenter is so we can rotate the playerCenter to match the player model, then we reference the player itself. We then have 3 references to LayerMasks for our collision, the large and small obj layers will be to decipher between the type of object we are holding and the interactLayer is for interacting with dialogue and tasks which we wont talk about here. We then have an assortment of bools that we use for checks and then we have what we need for the player inputs.
In start we have this code so that we can actually use the input grabAction that we want.
The first thing we do is create a LayerMask that stores the LayerMask of the object we are colliding with. We then check if the layer of the object we collided with is a big or small object, if it is either then we can pick it up which we do by making our heldObj into the object we collide with. Next we check which layer it has between small or large and change the isObjLarge bool accordingly. Finally we check if the grab action is pressed and then we have a bool called canToggleSetter which if it is true we start a coroutine which toggles between us being able to pick up objects and dropping the object, that coroutine looks like this.
The reason I used a coroutine to basically have a cooldown on the function being able to be used is because the .IsPressed() method is called every frame the button is pressed so it would call it multiple times no matter what so I added a artificial cooldown (I also am aware of WasPressedThisFrame() which only calls it once, but I was having trouble with it sometime not calling so I went with this instead).
Next we go to update to call our MoveObject and DropObject methods. But first we make the playerCenter rotation equal to the playerModel rotation. Then for actually calling the methods we lock them behind a bool which is the bool we set in the ToggleDelay IEnumerator. This is how we make it be toggle instead of a hold and release based pickup, we also check if we are actually holding an object by checking if the heldObject is null (this will make sure we wont try to pick up an object that doesn't exist while also preventing errors).
In the Move Object script we are doing very simple things, first we make the heldObjects position equal to the placeToHolds position. Next we make the heldObject kinematic which makes it so the object wont collide with other objects while held, in the same light we make it so the heldObject ignores the collision of the player so it doesn't effect that players movement.
Now for the DropObject we turn off the ignore Collision so that the player can interact with object outside of holding it. we make the isObjHeld false and we also make sure the held object is not kinematic.
That is basically everything from an object pickup perspective, we do other things in the script but they don't involve actually picking up the object so we will ignore it.
Now back in the editor you have to make sure you add everything to the inspector like the Place to Hold, Player center, and the Player. You then make sure the layer dropdowns have the correct layers selected
This is the final result, You can walk around to any object with one of the pick up layers and pick up the object which gets rid of its physics and then drop it on the ground activating its physics again. It might look a little strange just floating but once we get the pick up animation in we will just move the placeToHold to be on its hand during the animation making it more fluid.
Thank you for reading along and I hope I could help anyone trying to learn.
-Josiah
Get Not so Trash Panda
Not so Trash Panda
You're a raccoon just trying to do the right thing.
Status | Released |
Authors | Dylon999, jojoman941, LauraG914, slleggott, Navebackwards, Lord Unchemael |
Genre | Puzzle |
Tags | Casual, Cute |
More posts
- How To Throw ObjectsJul 23, 2024
- Using empty game objects as target points.Jul 22, 2024
- Common Mistakes when coding GamesJul 15, 2024
- 2D Art and UIJul 08, 2024
- How to make a water sprinkler with unity's Visual ScriptingJul 08, 2024
- What is Not so Trash Panda?Jul 01, 2024
- How to: Cloud ShadowsJun 25, 2024
- Completion Detection System for Unique TasksJun 18, 2024
- Creating the Cute Protagonist for Our GameJun 10, 2024
Leave a comment
Log in with itch.io to leave a comment.