- Home /
Drag Rigidbody
I consulted search engine after search engine, forum after forum. I just can't find a suitable answer to this question!
Is there a script that I can use, which allows the player to drag a rigidbody when it is clicked and dragged? I need this for a game, which you need to drag objects to create a bridge, allowing gravity to rotate it naturally. I don't need to rotate it, just be able to drag it. Thanks in advance!
Good luck finding a script. It depends a lot on how your game is setup and therefore, a generic script is probably not an option. If you did find one, it would probably be big and bulky and near impossible to use. But, it shouldn't be too hard to write your own script.
The script will need a drag state(ie: bool isDragging). You will need to raycast from the mouse position on the camera when the player clicks. The point where the ray hits the objects collider will be the object's rigidbody's center of mass during the drag. That way, it will rotate around the drag point.
It may be useful to have a control script to handle input,raycasting, etc. It can also store the object currently being dragged.
From looking at your profile, none of your 15 questions have selected answers. Either none of them have good answers or you are forgetting to select them. If any of them have decent answers, please remember to select them.
Have you tried the DragRigidbody.js script that comes with Unity? You can get it by Assets > Import Package > Scripts. If you've tried it, how is the behavior you want different from this scripts behavior?
Answer by ArchAngelus · Feb 15, 2014 at 07:23 PM
This was answered here: http://forum.unity3d.com/threads/56460-Drag-Rigidbody-script-works-but-passes-through-collider
If DragRigidBody doesn't work for what you need, try using Erich5h5's DragObject script: http://wiki.unity3d.com/index.php?title=DragObject
I'm actually going to attempt this myself now.
Answer by MegaMagaretha · Jan 27, 2016 at 05:43 AM
Here, i wrote a very simple script for dragging any Object with the rigidbody component. I hope i could help you.
using UnityEngine; using System.Collections;
public class DragRigidbody : MonoBehaviour {
public float catchingDistance = 3f;
bool isDragging = false;
GameObject draggingObject;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
if (!isDragging)
{
draggingObject = GetObjectFromMouseRaycast();
if (draggingObject)
{
draggingObject.GetComponent<Rigidbody>().isKinematic = true;
isDragging = true;
}
}
else if (draggingObject != null)
{
draggingObject.GetComponent<Rigidbody>().MovePosition(CalculateMouse3DVector());
}
}
else {
if (draggingObject != null)
{
draggingObject.GetComponent<Rigidbody>().isKinematic = false;
}
isDragging = false;
}
}
private GameObject GetObjectFromMouseRaycast()
{
GameObject gmObj = null;
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
if (hitInfo.collider.gameObject.GetComponent<Rigidbody>() &&
Vector3.Distance(hitInfo.collider.gameObject.transform.position,
transform.position) <= catchingDistance)
{
gmObj = hitInfo.collider.gameObject;
}
}
return gmObj;
}
private Vector3 CalculateMouse3DVector()
{
Vector3 v3 = Input.mousePosition;
v3.z = catchingDistance;
v3 = Camera.main.ScreenToWorldPoint(v3);
Debug.Log(v3); //Current Position of mouse in world space
return v3;
}
}
yes! this is really helpful, but i have a question. is there a way to let the object your dragging still collide with things? its just going through. sorry, i know this is kind of old.
Your answer
Follow this Question
Related Questions
2D Rigidbody Drag with Mouse 0 Answers
DragRigidbody - move object in Local Space, not World Space 1 Answer
"Launch" a rigidbody 3 Answers
How to properly convert mouse pos to world pos 1 Answer
newbie: drag a rigidbody with mouse 2 Answers