- Home /
Box collider on a moving object-shopping cart
Hello all,
Hope this is a new question: My goal in the scene is to have FPcharacter to pick a product and place in the cart. For that I've made all my "products" have box collider (for ray cast) and rigidbody (for behaving like in the real world). The cart also has box colliders, for the products to "stay" in the cart, 5 cubes with box colliders surrounding the cart (see picture) and made transparent for esthetic reasons.
Once I put the object in the cart it starts: 1. being all edgy and restless and jumpy like. 2. it doesn't stay there once the cart is moved, it slips away and falls. I've checked the colliders on the cart it's hermetically tight, no open "holes". I've tried playing with drag, mass, angular drag on Rigidbody.
How come the object is "falling" out of the cart?
Many thanks for your suggestions.
I always tell people to stay away from any type of collision physics. Your going to have to add some logic to your shopping cart, you can't rely on physics to calculate the objects in your cart.
Your problem your having is there is too many calculations your trying to preform and the physics on the box collided is calculating verts outside thus, allowing things to push through your shopping cart collides. Even if you got it working perfectly on your machine there is no guarantee on a slower machine these same things won't happen.
You will most likely have to implement some sort of transform fix to hold your objects in your cart.
Take a look at how unity calculates collider physics and you will understand.
Hello jmgek,
When you say logic do you mean scripting? I've managed to make the product "sit" calmly in the cart. But it still falls. This happens because, while the cart rotates, left\right (the x axes), the product wouldn't move with it (like when you have a sharp turn with your car, and your body seem to go the opposite way?). When the product is a child object of the cart then it keeps in the cart, of course. So I have a dynamic interaction between the cart (which is now a rigidbody with box collider) and a product - rigidbody with box collider. Perhaps I should use something like AddForce? to add force to the product?
Still looking to find some good materials about how unity calculates collider physics....
Yes, you will have to add additional scripts to hold your objects in the cart. A quick fix is to just gameobject.transform.positon = shopingCartBox.position;
Could I ask, whats the benefit to having object move once placed?
One way I can think of off the top of my head is to add a box collider inside the cart then use https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionExit.html On collision exit to add force once it's calculated when out of bounds.
Answer by AquaBomber · Feb 08, 2017 at 04:38 PM
Is your cart moving?If so you could do something like a "fake" product as a child of the cart with the product mesh.
Answer by Eco-Editor · Mar 01, 2017 at 07:15 AM
Hi jmgek,
once placed, the object need not move. I think what the approach should be is that: there's a collider, at the top of the cart, and it has a script on it that looks for a rigidbody (as all the products has this component) and once the product is picked and is "thrown" into the cart, it goes through the collider, and "looses" it's rigidbody features to become a child object of the cart, as it lands on the bottom. OnCollisionExit is an option here? How would you approach this?
Thanks.
Answer by AladdinHZ · Jun 30, 2017 at 08:49 PM
Hello @Eco-Editor, I'm facing the same issue what I've done is add Tag "CART" to the shopping cart, then attach script to the pick able object with the following:
Code :
void OnCollisionEnter(Collision col) {
Debug.Log ("ProductHandler : " + col.gameObject.name);
if (col != null) {
if (col.gameObject.tag == "CART" ) {
Debug.Log ("ProductHandler TAG : " + col.gameObject.tag);
// ADD INSIDE CART REMOVE RIGIFBODY
// SET COLLIDING AS PARENT
Rigidbody rg = GetComponent<Rigidbody>();
if (rg) {
Destroy(rg);
}
transform.tag = "inCart";
transform.SetParent (col.transform);
this.enabled = false;
}
}
}
so I remove the Rigidbody and set the cart as parent.
But I still have issue where my cart get through walls !!! Did u had this issue ?
Hi @AladdinHZ , I'm still trying to figure this out myself. One solution is to have a bigger character controller radius that will involve the cart, but it has it's downs.