- Home /
Moving a rigidbody onto exact mouse position using rigidbody.MovePosition?
Hello there,
my situation: I get the mouse position in 3D space while dragging the object, and it should move to this position. The script works very well so far, but the rigidbody object don't move to the exact mouse position. It recognises the mouse position, and flies to that direction... and it won't stop continue flying around, unless we release the mouse button.
And the important part of my drag script:
function FixedUpdate () {
if (dragging == true) {
rigidbody.isKinematic = false; var p: Vector3;
var mainCamera = FindCamera();
// Position on the near clipping plane of the camera in world space p = mainCamera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, mainCamera.nearClipPlane )); // Position relative to the eye-point of the camera p -= mainCamera.transform.position;
//[...] ...further distance calculation not necessary here... [...]
//finally, p holds the mouse position
//rigidbody.position = p;
var collisionMoveFactor = 0.1; p = p.normalized*collisionMoveFactor;
//the questionable part of this script: rigidbody.MovePosition(rigidbody.position + p);
// only move in its x,z coordinates, rememberedYPos = 1 rigidbody.position.y = rememberedYPos;
} }
So, I think the mouse position will always be added to the object position... How to avoid this, and only move it to the mouse position?
Edit For the complete script see my other question
Answer by tylo · Aug 26, 2010 at 06:39 AM
Try calculating a heading and using the rigidbody.AddForce method.
Vector3 heading = (p - transform.position).normalized;
rigidbody.AddForce( heading * someThrustValue * Time.deltaTime);
Sadly I've never tried to do anything with the camera and the mouse just yet, so I could be completely off base.
EDIT:
When you're obejct has reached the desired position, raise the rigidbody's drag value to stop it from moving.
if( transform.position == p )) //I think p is what you are using to determine the point in space you want the object to move too.
{
rigidbody.drag = 1000;
}
Give that a shot.
EDIT 2: Electric Boogaloo
You've shown that your results do not have the cube staying with the cursor as it moves, but rather it tries its best to keep up with it. Hopefully this code block will fix that with a different method of moving objects.
transform.position = new Vector3(p.x, transform.position.y, p.z);
I believe that will cause your cube to stay precisely with the mouse pointer as it moves around the screen without it flying in the air.
thanks for the reply! I have tried your script, and the object moves a bit more controlable than with my $$anonymous$$ovePosition code. Now the new behavior is also problematic. The object try to move in the direction of the mouse, thats fine, but it accelerates to it, moves beyond the mouse, slows down, turns, accelerates to the mouse and so on. That's only when I not change the mouse position, otherwise, it circles around the mouse until it finally crashes into a wall and then accelerates again. Any suggestions to fix this or to slow the object down if it gets near the mouse?
Sorry for the late reply. Yes I do have a solution for this. You can retard the velocity by raising the rigidbody's drag value.
So, when your object is now where your mouse is pointing, raise the rigidbody drag value to a value that will stop it, like 999999 or something less ridiculous.
Thanks for that, I gave you an upvote for your efforts ;) I used the mouse over+exit functions. The drag value fixes the problem if I move the object in a straight line, but if I change the mouse position more often it keeps circling. $$anonymous$$aybe this http://baronium.bplaced.net/Unity/rigiddrag2.html will say more than I can express.
Sorry for my rethink, but I think it would look better if the object is always under the mouse, and not moving. This would solve this problem in addition. $$anonymous$$aybe this is a step into another direction of scripting, but I also tried the rigidbodydrag asset script to include the collisions I need, without the success I've wished, and the objects are moving with this script too. Also I tried to set the object position directly using transform.position, that gave me the result I wanted, but as you surely know, that does not include the walls very well. Thanks very much in advance
Well, I am glad to see that at the very least you have a functioning script going! Yes, I can see why the object is doing that. Forces may not be the best solution here, then. $$anonymous$$y suggestion is that you directly update the cube's transform to the coordinates of where the mouse is pointing, then. This should keep the cube with the mouse pointer at all times. I will try and update my answer with a code block.