I posted this another (incorrect) thread, so I apologize if this seems familiar, but so far no answer so I’m moving to the correct location. I’ve been struggling to get a simple drag and drop demo working despite following the multiple tutorials linked here in the forums. My setup is straightforward. I have a sphere, a plane, and a first person controller. I’d like to be able to click the sphere, and drag it around the screen (in 3 dimensions), following where the mouse pointer is. The reason I can’t use the built-in DragRigidBody script is bc that results in a lot of lag (drag?), the sphere follows the mouse pointer slowly. So, I adjusted the DragRigidBody script to the following (removed all of the spring joint code) and placed this on the sphere:
function Update ()
{
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown (0))
return;
var mainCamera = FindCamera();
// We need to actually hit an object
var hit : RaycastHit;
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;
StartCoroutine ("DragObject", hit.distance);
}
function DragObject (distance : float)
{
var mainCamera = FindCamera();
while (Input.GetMouseButton (0))
{
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
transform.position = ray.GetPoint(distance);
yield;
}
}
function FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}
Dragging does work with this script, except that when I drag the sphere through a plane (e.g. a floor or wall), for some reason the sphere goes through the plane (which does by default have a collider attached to it), instead of bouncing off of it or at least stopping to move, which is what happens when i drag the sphere into another object like a cube. Does anyone know how to simply be able to drag and drop objects? It’s driving me crazy.
For the sphere to not go through the plane, you will need to move it using physics in the FixedUpdate() function. It’s dropping through when you drag it because you are giving it an explicit position to move to the next frame, rather than allow the physics system to move it. If the physics system doesn’t move the object, it can’t know how to make it interact with other objects.
I assume you have a rigid body on the sphere already, so move it like this :
function FixedUpdate()
{
var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
rigidBody.MovePosition(ray.GetPoint(distance));
}
Now it also looks like you’re starting your Coroutine there every frame in Update(). Update and FixedUpdate will only run if the component is enabled, so if you have a separate script polling input, and when dragging enables your other script, you should be able to do this without using coroutines.
Thank you for the reply. I’m confused as to why I would have to use the fixedupdate method, since the DragRigidBody code that came with Unity does all its work in update and the co-routine and that seems to work. With that said, I’m happy to try anything, and I still couldn’t get the following to work. I had to add the check to make sure the mouse was over “mysphere”, otherwise the movement would take place anytime I moused over a collider.
function FixedUpdate()
{
var mainCamera = FindCamera();
//We need to actually hit an object
var hit:RaycastHit;
if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100)) {
if (!hit.rigidbody || hit.rigidbody.isKinematic) {
return;
} else {
if (hit.rigidbody.CompareTag ("mysphere")) {
var distance = hit.distance;
var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
rigidbody.MovePosition(ray.GetPoint(distance));
}
}
);
}
What happens with this code is when I mouse over mysphere, the sphere moves to the camera very quickly over and over, instead of just smoothly following where the mouse is in space.
Have you tried using DragRigidBody but with different settings for the spring and drag values? You might be able to make the dragged object move faster with these.
Thanks for the reply, I have tried adjusting the settings on the DragRigidBody script, but to no avail.
I couldnt get the code posted by Murcho to work either… I’m assuming there HAS to be a way to drag and drop things in Unity in the way I described above, I can’t be the first to need this functionality. Again any help is appreciated, I’m busy working away at it myself.