- Home /
Add Score for every Object inside a Box
I want others perspective on this kind of approach. I have a container, a box. There are three items, Baseball Bat, Racket and Ball. What happens is, There will be added 5000 points for every item that is put inside the container. How should I go with this? Any thoughts? much appreciated.
Answer by Mahir-Gulzar · Aug 29, 2018 at 10:38 AM
A very simple way would be to place a trigger on the open side of the box.. Give a specific tag to items which you can put inside that box.. and just detect if the such item has entered the trigger and disable dragging functionality of that item onTriggerEnter and let it fall inside the box enabling the rigidbody component of it. PS: Put the detection and points increment script on the box.
Answer by Ermiq · Aug 29, 2018 at 04:19 PM
As an option: Physics.OverlapBox
. So you can check what's inside the box right now and there's no need to detect in-out triggers:
public Transform theBox;
void CheckWhatsInside () {
Vector3 boxCenter = theBox.position + theBox.up * theBox.localScale.y / 2f; //actual calculations depend on where is the pivot point of the box
Collider[] stuffInTheBox = Physics.OverlapBox (boxCenter, theBox.localScale / 2f, theBox.rotation,
Physics.AllLayers, QueryTriggerInteraction.Ignore);
foreach (Collider col in stuffInTheBox) {
if (col.transform && col.transform != theBox) {
// +5000 points for this thing!
}
}