Search Unity

  1. Unity 2018.3 is now released.
    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. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    56
    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,537
    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:
    56

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

    qkson

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

    AndersMalmgren

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

    qkson

    Joined:
    Jan 15, 2018
    Posts:
    56