- Home /
How to deal with collision by using mouse drag?
I'm doing a 15-puzzle game. It's probably not the most clever way to do but I'm using RigidBody for the cubes. The player moves the cube by dragging them with the mouse.
The problem is no matter what I do, one of the cube will eventually move through another one. All the cubes use continuous collision detection and I'm using MovePosition, but it still don't work.
Here's my function (beware, it's crappy code) :
function OnMouseDrag(){
var hit : RaycastHit;
//keep track of the mouse position
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//convert the screen mouse position to world point and adjust with offset
curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
if (curPosition.x - 0.1 > transform.position.x ){
if(rigidbody.SweepTest (transform.right, hit, 0.02) == false){
rigidbody.MovePosition(rigidbody.position + Vector2.right * (1) * Time.deltaTime);
}
}
else if (curPosition.x +0.1 < transform.position.x){
if(rigidbody.SweepTest (transform.right, hit, -0.02) == false){
rigidbody.MovePosition(rigidbody.position + Vector2.right * (-1) * Time.deltaTime);
}
}
else if (curPosition.y -0.1 > transform.position.y){
if(rigidbody.SweepTest (transform.up, hit, 0.02) == false){
rigidbody.MovePosition(rigidbody.position + Vector2.up * (1) * Time.deltaTime);
}
}
else if (curPosition.y +0.1 < transform.position.y){
if(rigidbody.SweepTest (transform.up, hit, -0.02) == false){
rigidbody.MovePosition(rigidbody.position + Vector2.up * (-1) * Time.deltaTime);
}
}
}
From the Assets menu, select Import Package/Scripts. Then play with the DragRigidbody script that comes in as part of this package. It uses joints and does not have the issue you have by using $$anonymous$$ovePosition().
That's what I used first, I don't remember why I scraped it, anyway the problem is the same, the collision are even more jiggly. Just to be sure, I just have to apply the script on all the rigidbody I wan't to move and that's it ?
Also I forgot to say that I need the objects to be kinematic, I don't want them to move when the player is not touching them.
Ok, I've managed to bypass the kinematic limit with this :
function On$$anonymous$$ouseDown(){
this.rigidbody.is$$anonymous$$inematic = false;
}
function On$$anonymous$$ouseUp(){
this.rigidbody.is$$anonymous$$inematic = true;
}
But now I'm back from the start, I can still make objects go trough each other.
DragRigidbody is applied to only one game object. Create an empty game object and attach it. It will work for all the rigidbodies in the scene. Note is is fairly straight forward to modify this script so that it will turn the iS$$anonymous$$inematic flag on and off so you will not have to add the script above to all your objects. You may have to alter the spring values and the attach point to get the drag behavior to be what you want.
Note by default object will inter-penetrate slightly. If this is the issue you are trying to work around, go to Edit/Project Settings/Physics, and '$$anonymous$$in Penetration For Penalty' value to 0.0.
Editing the project setting greatly improve this. Thank you !
Your answer
Follow this Question
Related Questions
Collision with object using MouseDrag 1 Answer
Throw an object on collision with mouse cursor 1 Answer
Dragging Rigidbodies goes through colliders 3 Answers
Dragging Rigidbodies goes through colliders on non linear paths. 2 Answers
How to move objects away from each other (not what you think) 1 Answer