- Home /
Question by
gr8sunny · Jun 06, 2012 at 12:03 PM ·
collisionrigidbodybox collider
Parenting a box collider object with a rigid body - not able to unparent
I have a "flower pot" which is a rigid body and a "flower" which has a box collider. Both can move with mouse when dragged(see the following scripts). The flower should become child of flower pot when they collide so that the flower also moves along with the flower pot. The flower is tagged as "Finish". This works fine. But, when I try to move only the flower, the pot should not move with it, but this is not happening. When I click on flower it is treating it as a click on flower pot. The flower pot has following script attached to it-
var move_with_mouse : boolean = false;
var original_y :float;
var original_x :float;
function Start () {
original_y = transform.position.y;
original_x = transform.position.x;
}
function Update () {
if(move_with_mouse)
{
rigidbody.velocity = Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x-transform.position.x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y -transform.position.y,0) * 10;
if( transform.position == Camera.main.ScreenToWorldPoint(Input.mousePosition) )
{
rigidbody.drag = 9000;
}
}
else
{
//transform.position.x = original_x;
}
}
function OnMouseDown(){
move_with_mouse=true;
}
function OnMouseUp()
{
move_with_mouse=false;
}
function OnCollisionEnter (other : Collision) {
if(other.gameObject.tag == "Finish") {
other.transform.parent = transform;
}
}
function OnCollisionExit (other : Collision) {
if(other.gameObject.tag == "Finish") {
other.transform.parent = null;
}
}
The flower has following script-
var move_with_mouse : boolean = false;
var original_y :float;// =transform.position.y;
var original_x :float;
function Start () {
original_y = transform.position.y;
original_x = transform.position.x;
}
function Update () {
if(move_with_mouse)
{
//Make 'x' and 'y' coordinates of flower equal to mouse position
transform.position.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
var mouse_y:float = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
transform.position.y = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
}
else
{
}
}
function OnMouseDown(){
move_with_mouse=true;
transform.parent = null;
}
function OnMouseUp()
{
move_with_mouse=false;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613072429im_/https://answers.unity.com/themes/thub/images/avi.jpg)