- Home /
RigidBody2d not detecting collision when dragged
Hello, Here is a gif of what my issues is:
link:View GiF
As you can see on click I spawn in a prefab which has a RigidBody2d and a circle collider 2d. It expands until you release the click and then drops down. I have borders which stop the circles from falling out of camera but they dont seem to collide with my circles when I'm dragging them.
Here is my code for dragging, spawning and expanding the circle and the script is attached to the camera:
function Start() {
}
var objectToInstantiate: GameObject;
private
var myCurrentObject: GameObject;
function Update() {
if (Input.GetMouseButtonDown(0)) {
myCurrentObject = Instantiate(objectToInstantiate, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity);
}
if (Input.GetMouseButton(0) && myCurrentObject) {
myCurrentObject.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
myCurrentObject.transform.position.z = 0;
myCurrentObject.transform.localScale += new Vector3(0.1F, 0.1F, 0);
}
if (Input.GetMouseButtonUp(0) && myCurrentObject) {
myCurrentObject = null;
}
}
I hope this all makes sense, Thanks in advance!
Answer by Paricus · Dec 30, 2016 at 03:24 PM
the physics engine won't take into account the change in scale because its not a physics based movement. try adding a bool to the if (Input.GetMouseButton(0) && myCurrentObject)
and when OnCollisionEnter()
is called, set the bool to false.
Ill give it a try, thanks. I dont think it will work as even without the scale I'm still able to drag the circles through the borders. Thanks
it should work as both scale and translate don't follow physics. on your collision function you could also add myCurrentObject = null;
to simulate what happens when you release the mouse button