Search Unity

Lifting/grabbing objects (2D)

Discussion in 'Physics' started by jilleJr, Feb 1, 2015.

  1. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    I want to lift an object in 2D space, be able to move it around, and release at will. BUT also the player should not be able to stand on it while you're holding it.

    I've been struggleing with this for a while and I can't figure out how to acomplish this...

    This is my small script for seeing if the object is grabbed, i.e. picked up and about to be moved:
    Code (CSharp):
    1. private bool isGrabbed = false;
    2.  
    3. void OnMouseDown() {
    4.     Debug.Log ("You grabbed " + name + "!");
    5.  
    6.     isGrabbed = true;
    7. }
    8.  
    9. void Update() {
    10.     if (!Input.GetMouseButton (0) && isGrabbed) {
    11.  
    12.         Debug.Log("You released " + name + "!");
    13.  
    14.         isGrabbed = false;
    15.     }
    16. }
    Works all fine and well.
    Now on to the moving of the object...
    Code (CSharp):
    1. void FixedUpdate() {
    2.     if (isGrabbed) {
    3.  
    4.         // Get the mouse location on screen
    5.         Vector2 rawMouse = Input.mousePosition;
    6.  
    7.         // Get the mouse location in the world
    8.         Vector2 mouse = Camera.main.ScreenToWorldPoint (rawMouse);
    9.  
    10.         Vector2 deltaDist = mouse - new Vector2(transform.position.x, transform.position.y);
    11.         Vector2 direction = deltaDist.normalized;
    12.  
    13.         Vector2 force = direction * speed;
    14.  
    15.         rigidbody2D.AddForce(force);
    16.  
    17.     }
    18. }
    Where speed (from row 13) is a public float variable.

    Now I get some odd behaviour with this like the object I'm holding makes a tiny orbit around the mouse.
    If I could get rid of that I think the rest would be fairly simple to solve.

    I think to make it that the player can't stand on the object while I'm holding it I can just increase the players mass right?
     
  2. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    jilleJr likes this.
  3. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Thanks for the help, a bit late reply but I ended up going for a different solution:
    Code (CSharp):
    1. rbody.AddForceAtPosition(deltaDist * speed,transform.position)
    It kinda works as I want it to, and will have to do.
     
  4. TheGhostlyGuy

    TheGhostlyGuy

    Joined:
    Jan 16, 2020
    Posts:
    1
    So did you replace the first line or add it?
     
  5. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    That line was placed inside the if statement within the FixedUpdate method, instead of
    Code (CSharp):
    1. rigidbody2D.AddForce(force);
    So yes, replaced.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    I understand this is from a very old post but I wanted to add the following for information.

    You can use the TargetJoint2D for this.

    You can go to my repo which demonstrates many 2D physics features here and load the TargetJoint2D_MouseDrag scene. As per the docs you can control the maximum force and how stiff it is so you can tweak the behaviour from a springy hold to a rock solid hold. You can also connect to the center of mass so you don't induce torque and therefore rotation or any point away from the center of mass so it moves appropriately.

    You can see that scene working here. It needs hardly any code to work. If you look closely you can even see your forum post on the taskbar. ;)
     
    jilleJr likes this.
  7. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Smart marketing :p and yes very nice with some fresh knowledge to an old thread.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    If by "Smart marketing" you mean the UnityPhysics2D project then I feel compelled to add that it was all done in my own time for the pure love of it as is my helping nice folks like you on the forums! :)

    Every person it helps makes it more worthwhile.
     
  9. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Yes absolutely! I was not bashing, but instead thinking it was clever to run around in forums and suggesting your prepared solution instead of others having to manually find it through GitHub or whatev
     
    MelvMay likes this.