Problem with picking up object

Problem with picking up objects.

Hello, this is my first time posting here. This is also my first time posting in ANY forum, so sorry if I do something wrong and/or being confusing. I am also a beginner at coding.

What im trying to achive is to be able to pick up a object (in game) with my mouse and the objects middle not snaping to the mouse/pointer. I want this and for the object to be “effected by gravity” when holding the object. So for example: if i pick up a object from its side it would not snap to the objects middle and would swing around since it is not the center of the object.

BTW this is a 2d game, using unity version 6000.4.0f1

From what i have understood is that you can use joints to achieve this. I have seen people use spring joints but the specific tutorial i followed used hinge joints. The object can at this point be thrown around the screen. I have noticed that the hinge joint is created where you clicked on the object but its middle still snaps to the mouse. I also get a warning that the hinge joints connected rigid body cannot be connected to itself. I dont know if that is related.

The tutorial i followed: https://www.youtube.com/watch?v=6DUiuAL-Uls

My script (it is a modified version of the script in the tutorial)

using UnityEngine;

public class Drag_Script : MonoBehaviour
{
    GameObject HingePoint;
    HingeJoint2D hinge;
    public Rigidbody2D rb;
    
    Vector2 vel;
    Vector2 lastPos;
    Vector2 objPos;

    public bool moving = false;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();

        

        HingePoint = this.gameObject;
        
    } 

    private void OnMouseDown()
    {
        Vector2 mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

        HingePoint.transform.position = new Vector2(Camera.main.ScreenToWorldPoint(mousePos).x, Camera.main.ScreenToWorldPoint(mousePos).y);

        hinge = HingePoint.GetComponent(typeof(HingeJoint2D)) as HingeJoint2D;

        moving = true;

        hinge.enabled = true;


        hinge.connectedBody = rb;
        hinge.autoConfigureConnectedAnchor = false;


        hinge.anchor = transform.position;


        rb.linearVelocity = Vector2.zero;
        rb.gravityScale = 0;

        Debug.Log("Mouse down on object");
        //set velocity to 0 when picking up object
        //set gravity to 0 when picking up object. Fixed object drooping 
    }

    private void FixedUpdate()
    {
        if (moving == true)
        {
            Vector2 mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            HingePoint.transform.position = new Vector2(Camera.main.ScreenToWorldPoint(mousePos).x, Camera.main.ScreenToWorldPoint(mousePos).y);



            objPos = transform.position;

            vel = (objPos - lastPos) / (Time.fixedDeltaTime);

            lastPos = transform.position;

        }


    }

    private void OnMouseUp()
    {
        hinge.connectedBody = null;

        hinge.autoConfigureConnectedAnchor = true;

        hinge.enabled = false;

        moving = false;

        rb.linearVelocity = rb.linearVelocity + vel;

        rb.gravityScale = 1;

    }

}

the warning
Screenshot 2026-06-11 135405

a screen recording

Please help me and please tell me if you dont have enough information. If im in the wrong place please tell me that to :slight_smile:

Welcome! The cool code blocks are the little </> button at the top of the editor box.

Nobody will retype code from an image to comment or adjust, so it’s important you use them.

Joints have to either connect to nothing (which makes them connect to an infinitely strong point in the ether), or another body, not themselves.

That error sounds like you didn’t quite correctly follow the part of the tutorial that sets up the connection at pickup time, so focus on that area again.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Step #2 is particularly critical when learning.

If you are unwilling or unable to do Step #2, just ask someone else to do the whole game for you.

Thanks for the reply and thanks for telling me how to create the code blocks! The video is not exacly a tutorial, more of a showcase (i know i stated that it was sorry). I tried to copy and paste the code (the video has a google drive with the code) and tried to set it up the same way the person in the video did (they did not really show how it should be set up). I still had the problem, and an additional one because they use static rigid bodies in the video and i get a warning saying linearVelocity cannot be used with a static body. This was a very old video and may not work in the newer versions of Unity, do i need to take that into account when researching? I know about the old and new input systems and such, but is there more differences between the old and new?
This was the only video i found that did what i wanted and if i it does not work, what should i do?

It sounds like you’re making a basic “Gravity gun” mechanism and I know there’s a lot of tutorials for those around the internet.

The basics of gripping something can be done with various types of joints to achieve various results. I think the OG gravity gun was just floating free, no control on rotation, or perhaps only weak control.

At the end of the day, you can “add” gravity gun functionality to any kind of controller, usually by having an invisible proxy GameObject out in front of the character that things attach to on command, where “attach to” could be any number of mechanisms ranging from parenting to a joint or even to code that “drives” the position there in some way, and perhaps even addresses the rotation.

Thank you again! I didnt notice that it is a lot like a gravity gun. Now i know where to look!

You may want to work through a tutorial (or two) but do it precisely like they do, then use that as a “I know this works” baseline to tinker with other ways of holding things.

If you just want a handy player controller (something that is surprisingly difficult in itself), here’s two to start from that could easily have a gravity gun grafted onto them:

Here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

That one has run, walk, jump, slide, crouch.

And here is a free Kinematic Character Controller that also comes with a huge playground: