How do you pick up an object with a tag? [must have physics and no inventory]

So I’m making a game which has an amnesia style system of object interactivity including cabinets / drawers opening. However after about 6 hours of searching I couldn’t find a single good code that worked fully. Also i would like the object to rotate via mouse.

[ex. Garry’s mod holding E with something in your physgun makes it rotate]

currently i have a crosshair script which has a function for “firing” from the midpoint of the screen.

Please help. I’m very new to coding in unity and extremely new to Javascript / C#

Having never played it, I watched a short clip of the Video Game. For some objects (like dragging a table around), it looked like what would happen if you used the standard DragRigidbody.js script. You can get it:

Assets > Import Package > Scripts

Other appeared that they may not be using the physics (like opening a door). Note in a 3D game sometimes mapping the 2D mouse into the scene can be difficult. I have a bit of started code for someone who wanted to open a door with dragging here:

http://answers.unity3d.com/questions/614173/drag-door-where-to-start.html

Ok, I made an Amnesia clone a while back using coroutines like this for rigidbody interaction so this should behave how you wanted, it pulls objects from their center rather than a single point like with the spring joint method. It just needs an empty parented to the camera.

public Transform player_camera;
public Transform phys_dragger; //empty parented to camera
public float grab_distance = 5.0f;
private float temp_drag = 10.0f;

IEnumerator DragObject(Rigidbody r){
    float oldDrag = r.drag;
    r.drag = temp_drag;
    r.useGravity = false;
    while(Input.GetButton("Interact")){
        //Keep aligned with camera's view
        r.transform.rotation = Quaternion.Slerp(r.transform.rotation, phys_dragger.rotation, Time.deltaTime * 15.0f);
	    //Vector that points from rigidbody to grab point
        Vector3 vel = (phys_dragger.position - r.position).normalized;
        //Clamp distance to 1, otherwise it could be pulled through walls
        float d = Mathf.Min(Vector3.Distance(r.transform.position, phys_dragger.position), 1.0f);
        r.AddForce(vel * d, ForceMode.VelocityChange);
		
        if(Input.GetButton("Throw")){
            r.rigidbody.AddForce(player_camera.transform.forward * 5, ForceMode.Impulse);
            r.drag = oldDrag;
            r.useGravity = true;
            yield break;
        }
        yield return new WaitForFixedUpdate();
    }
    r.drag = oldDrag;
    r.useGravity = true;
}

Then it would be used with a raycast so you can pass it the rigidbody that was hit without needing a permanent variable to store it in.

void Update () {
    if(Input.GetButtonDown("Interact")){
        RaycastHit hit;
        if(Physics.Raycast(player_camera.position, player_camera.forward, out hit, grab_distance)){
            if(hit.rigidbody){
                phys_dragger.position = hit.rigidbody.position;
                phys_dragger.rotation = hit.rigidbody.rotation;
                StartCoroutine(DragObject(hit.rigidbody));
            }
        }
    }
}

You’ll want to mess around with the temporary drag and forces, if you lower the drag you’ll notice it will orbit around the point but if it’s high the object will move too slow.

Thanks dude! It took a bit of futzing about but i got it to work. oddly enough the paste bin version worked and the code in here glitched out the editor for a bit. however just with a bit of messing about and minor code patching i got it to work 100%

Thanks again for both awesome answers :smiley: