Hello. I'm currently am writing code for my video game where the player character has to pick up objects up certain items in order to solve puzzles and progress through the game, but I am currently having trouble finding what type of function I need to use in my program. Can anyone help me with this, please? I've looked for two days now, and I've only found a safer way to destroy the original object--but I have currently have not found a way to load the game object onto the player itself yet.
Hi, Do you want to add the item into inventory (and remove from scene, until player uses the item or drops it from inventory), or just stick the item to the players hand..?
Whoops. I should have made that clearer... I wanted to have the item hover on top of the player as they walked around with it.
Here's one very quick and dirty version for some ideas.. Basically when you set the transform as a child of the player, it follows him easily.. https://docs.unity3d.com/ScriptReference/Transform.SetParent.html source https://gist.github.com/unitycoder/d1725766928c308ba54d78209f0367cf
Thank you so much! I'll go ahead and fix up my code so that way the transform can work in my project! Do you mind if I use yours for reference?
no problem, and post here if there's some issues.. also maybe would be nicer to pickup items only when you press button..
I was thinking about using a key press to pick up objects! I've managed to finish off all of the code I needed, but I encountered a problem: when I tried testing the code on the player, I encountered an NullReferenceException. It says that an 'Object reference [is] not set to an instance of an object'. I don't know how to make the message disappear.
Maybe its the pickuppoint reference? here's the scene setup: empty gameobject for the carrying position
I've just tried that, it's still giving me the error. I think it might be my version of the carryLocation, as the item isn't being registered by the compiler AS my version of the carryLocation. This message auto-activates once my player walks onto the object itself.
From which line of code that error is? is it the: Code (CSharp): currentItem.position = carryLocation.position; Can you screenshot the mainCharacter inspector, so can check
Alright. I'm not sure where you want to look, so I'll open up the polygon collider as well. Edit: here's the sprite thing as well.
A easy way to do this is make the object = the players location + or - and set its x and y axis when the player intersect the object. I'm a Vb .Net dev so I would do a trigger based on interesting with its bounds but you guys are using scripts so you need something to trigger the player axis = the object axis and then perform your action for the object based on player location. Basically your issue is your collision detection.
mgear, the OnTriggerEnter2D is the exact same as yours, only with different labels and names. I haven't done anything different. mholmes, I'm not sure what you're saying. I know it's collision detection I'm having problems with, but I would like to better understand your solution you have there.
Alright, I'll go ahead and do that right now. Edit: I just found out where the Hold Item's location was, and I'm currently figuring out a way to get the transform to accept the game object. New Edit: I've managed to change the second Transform to be used on the mainCharacter instead of the script, now I'm having trouble with the program not putting the item onto the player. Final Edit: Just found out what my problem was and fixed it: I had my versions of the carryLocation and the currentItem mixed up in the last part of the code. I have since fixed it, and everything works!
Hi Mgear, I am using this code for my project. I made a few adjustments to the code as my project is 3D not 2D.. The issue i am having is once i drop the object it wont let me pick it up again. Also two other things i am trying to do with it that maybe you could help with. 1. If the object is brought within an area on the map the player wins. 2. only particular Characters can pickup the object. (Which i assume i can do this easy by only attaching the code file to the characters that can pick up the object)
add the gameobject as child of the hand, so it will automatically follow. https://docs.unity3d.com/ScriptReference/Transform.SetParent.html
hello i cant for some reason post on unity answers but do u know how to make a character pick u in 2d
basic idea is still the same, if you set transform as child, it follows player: but better check more complete tutorials from youtube, https://www.youtube.com/results?search_query=unity+2d+pick+up+object+