Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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:
    24
    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:
    3,154
    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:
    24

    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:
    3,154
    Ah it has some build in raycast logic that tests in center of screen?
     
  5. qkson

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    24
    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:
    3,154
    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:
    460
    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:
    24
    That makes sense. Thanks, I'll probably change it :)
     
  9. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    3,154
    That automagic for the pickup wont work though I guess