- Home /
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#
Answer by Josh707 · Jan 16, 2014 at 12:23 AM
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.
I tried Adding the code twice both in js and cs however on js i get too many errors and on cs i get 2 which i don't understand at all. =\
Hmm, I wrote that in C# so it won't work if you just paste it into a JS file. IEnumerator would be replaced with function and so on. $$anonymous$$ake sure you have the class name and using namespace directives at the top:
using UnityEngine;
using System.Collections;
public class grabber : $$anonymous$$onoBehaviour {
If that's not the problem I'm not sure what is, what version are you using? Here's a copy/paste of the working script in case I screwed something up
Answer by robertbu · Jan 15, 2014 at 12:01 AM
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
Thanks for the door tutorial, well i suppose that leaves me with only the item lifting and moving left
Did you try the DragRigidbody.js script? This allows arbitrary dragging of objects.
That helps quite a bit however i don't need it attached to the mouse but the center cross hair and i have no idea how to manipulate that, the view is first person
You can save a vector3 of the objects position when you first grab it, the easiest would be moving an empty gameobject which is a child of the camera so it rotates with your view, and calculate a vector from the object to the initial point scaled by the distance and use AddForce to push it there. If you look at the coroutine used in this script:
http://wiki.unity3d.com/index.php/Drag%26Throw
You can modify it to do what I described above, it works best if you temporarily increase the drag values or the rigidbody will move too fast and orbit around the point
It works for the most part but there's one problem [shown in image]
it's being held by the mouse which is the problem
Answer by DXOFTX · Jan 16, 2014 at 07:59 AM
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 :D