Search Unity

Picking up and carrying objects

Discussion in '2D' started by Aquillis, Jul 22, 2016.

  1. Aquillis

    Aquillis

    Joined:
    May 30, 2016
    Posts:
    13
    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.
     
    UtopianKing likes this.
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    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..?
     
  3. Aquillis

    Aquillis

    Joined:
    May 30, 2016
    Posts:
    13
    Whoops. I should have made that clearer...:oops: I wanted to have the item hover on top of the player as they walked around with it.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    LayalHF and UtopianKing like this.
  5. Aquillis

    Aquillis

    Joined:
    May 30, 2016
    Posts:
    13
    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?
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    no problem, and post here if there's some issues..

    also maybe would be nicer to pickup items only when you press button..
     
    Carloscastano likes this.
  7. Aquillis

    Aquillis

    Joined:
    May 30, 2016
    Posts:
    13
    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.
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    Maybe its the pickuppoint reference?

    here's the scene setup:
    upload_2016-7-23_12-18-34.png

    empty gameobject for the carrying position
    upload_2016-7-23_12-17-9.png

    upload_2016-7-23_12-17-55.png
     
  9. Aquillis

    Aquillis

    Joined:
    May 30, 2016
    Posts:
    13
    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.

    null reference.PNG
    This message auto-activates once my player walks onto the object itself.
     
  10. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    From which line of code that error is? is it the:
    Code (CSharp):
    1.  currentItem.position = carryLocation.position;
    Can you screenshot the mainCharacter inspector, so can check
     
    Gozooheck likes this.
  11. Aquillis

    Aquillis

    Joined:
    May 30, 2016
    Posts:
    13
    Alright.
    Check.PNG
    I'm not sure where you want to look, so I'll open up the polygon collider as well.
    Check2.PNG

    Edit: here's the sprite thing as well.
    Check3.PNG
     
    Last edited: Jul 24, 2016
  12. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    Looks fine, then what code you have in the OnTriggerEnter2D? Or is it just the same as mine?
     
  13. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    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.
     
  14. Aquillis

    Aquillis

    Joined:
    May 30, 2016
    Posts:
    13
    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.
     
    Last edited: Jul 24, 2016
  15. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    Can you show the full script, just for checking..
     
  16. Aquillis

    Aquillis

    Joined:
    May 30, 2016
    Posts:
    13
    Alright, I'll go ahead and do that right now.
    Check4.PNG Check5.PNG Check6.PNG Check7.PNG



    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.
    Found it.PNG

    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!
     
    Last edited: Jul 30, 2016
  17. jmattarock_unity

    jmattarock_unity

    Joined:
    Mar 10, 2018
    Posts:
    25
    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)
     
  18. LeeStark

    LeeStark

    Joined:
    Dec 26, 2019
    Posts:
    1
    How do I get it to stay attached to player's hand
     
  19. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
  20. copshawbutch

    copshawbutch

    Joined:
    Dec 27, 2020
    Posts:
    1
    hello i cant for some reason post on unity answers but do u know how to make a character pick u in 2d
     
  21. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448