Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Amnesia Type Drag Script

Discussion in 'Scripting' started by ZBlade, Sep 19, 2012.

  1. ZBlade

    ZBlade

    Joined:
    Sep 15, 2012
    Posts:
    53
    Hey guys, For all people who have played amnesia would know about the ability to move stuff like barrels and furniture
    I would love to have that feature in my game I am creating in which you will have to move such objects to get to different places like so
    but i do not have a code to do that
    Any help with a code to pick up objects and move them around?
    NOTE: The code should be for selected objects only so theres no like picking up trees ect lol
    Replies with "working" codes and instructions on what to add the code to along with anything else needed will be greatly thanked

    - ZBlade
     
  2. typane

    typane

    Joined:
    Nov 25, 2011
    Posts:
    297
    I will not give you the code, there is no sense of accomplishment if you do not do it yourself or at the very most you are wanting most of this functionality handed to you. The script isn't very hard to create just think logically.

    Use raycasting to determine item, if item is able to be picked up display guiTexture hand. If left mousebutton and hold then create a math.Atan distance exact between you and the object, object follows mouse screen to world. Break everything down step by step don't tackle it as a large mechanic.
     
  3. ZBlade

    ZBlade

    Joined:
    Sep 15, 2012
    Posts:
    53
    I'm sorry but I am only a beginner with scripting :p
     
  4. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Use one of Unity's official tutorials and once you've got a handle on the basics, use Unity's official Scripting Reference to find what you need.
     
  5. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
    Pretty much have to agree with the previous replies.. if you still want somethng out of the box, then you should just look for tutorials or scripts.. someone has probably already made this so chances of finding it on the internet are pretty high.
     
  6. ZBlade

    ZBlade

    Joined:
    Sep 15, 2012
    Posts:
    53
    Right i had found a script, thanks anyway but theres a new problem, NOTE: the script will only allow objects with the tag "Pick" to be picked up;
    so basically I can pick it up and so using the "e" key but when I let go of the object it stays afloat and don't fall back to the ground
     
  7. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    give it a rigidbody. if having the rigidbody makes it do weird things, change the code so that when clicked, rigidbody is made kinematic, then non kinematic when released.
     
  8. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Wow, this thread is the most passive-aggressive response to this question I have ever seen. What's with the pile-on, guys? We're a community that helps noobs, remember?

    People ask for help creating Amnesia-type manipulation of physics objects roughly once a week. The short answer is to mess with the script located at Assets\Standard Assets\Scripts\DragRigidbody.js. This script was designed to make it possible for the player to click and drag objects around in a scene. You can see it in action in the realtime Shadows demo here: http://unity3d.com/gallery/demos/live-demos#shadows . It works by creating an invisible physics spring and attaching the clicked-upon object to this spring, allowing Unity's physics engine to do the rest automatically.

    Unfortunately, although this script technically gets the job done, it has noticeable flaws when applied to a typical First-Person Shooter setup:

    - The held object can take a long time to catch up to "where it should be."
    - The held object can appear to wobble or bounce back and forth in front of the camera like it's on a spring. Because, well, it is.
    - When the player walks forward, he can sometimes "bounce off of" the item he is holding.
    - Doors and other physics objects with a theoretically fixed hinge can appear to "break" off of their hinges, only to snap back to where they actually belong the moment the player lets go of the mouse button.
    - Objects picked up on the very edge of the player's reach or field of vision remain that far away from the player while they are being carried. In a game like Half-Life or Amnesia, they would tend to float directly in front of the player once grabbed.
    - When a fixed object such as a door is dragged, or when a carried object becomes "stuck" behind a fixed object (such as a crate being carried becoming stuck behind a pillar,) the player can back away from the object and it will continue to be "carried" by the player, even if they move several hundred feet away.
    - By default, the length of the ray cast by DragRigidbody.js is quite long. Reducing this to 1 or 2 is an easy, obvious fix, but it bears mentioning.

    I have never seen this problem completely solved in a satisfactory way in a public venue before. However, I have ALWAYS seen well-meaning regulars here on the Unity forum attempt to do EVERYTHING they reasonably can to help newbies solve this problem when it comes up.

    TL;DR: DragRigidbody.js is a good place to start, but it's not quite there yet. This is likely because it was never designed with a first person viewpoint in mind. Good luck, and if you figure out a way to do this well, I urge you to post the complete project to the wiki. I'm sure the hundreds of people who always ask this question would love to know how you did it.

    Failing that, honestly ask yourself if your game really *needs* physics puzzles, or if simple fixed key-and-lock puzzles would get the job done just as well.

    Good luck. And sorry about the earlier responses this thread got. We usually treat our noobs better than this.
     
    Last edited: Dec 27, 2012
    Alverik and CliffracerX like this.