Search Unity

Pick up, throw and drop an interactable object.

Discussion in 'Scripting' started by qkson, Nov 10, 2018.

  1. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77
    Hey guys, out of boredoom I made a simple script which allows an object to be picked up, and then throw it or drop it down on the ground. Here's the link to GitHub.
    https://github.com/Qkyyy/PickupablesObjects-UNITY
    Feel free to use it accordingly to your need. I encourage you to expand it, because it's only a basis for general good. Let me know what do you think about it, cheers!
     
  2. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    The seperation of concerned is really strange here, you let the object determine if it should be picked up, I would have put that logic on the player. Also if any object is within 2.2 it will be picked up so if 5 objects are within distance all 5 will be picked up.

    Also it will Clip objects, you should use force instead to move it so it can't clip the world.

    Just some quick things I saw when I looked at it quickly
     
  3. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77

    Thanks for the answer!
    Only one object will be picked up, because the code responsible for it is in OnMouseDown()
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Ah it has some build in raycast logic that tests in center of screen?
     
  5. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77
    From what I know it detects if raycast had hit the collider of the object.

    Well, I did not want to put that code in PlayerController, not to clutter up the script. If someone will add later interactions with other objects or some enemy damage, I think it would also be in separate scripts.
    But that's only my opinion, correct me if im wrong :)
     
  6. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    You don't need to put it directly on the player. You can add a component that does it.
     
    qkson likes this.
  7. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    This.

    I would do this because it allows you to have NPCs use the exact same code.
     
  8. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77
    That makes sense. Thanks, I'll probably change it :)
     
  9. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    That automagic for the pickup wont work though I guess
     
  10. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    77