How to select multiple objects based on center of a collider
This logic would work with any point within the game object. In other words, a collider is not required. You could instead have it set to the Transform.positin property of a game object or even your own arbitrary point.
The collider.bounds.center
property will be used to determine the center point of the selectable game object. If the player drags their selection box over said center point, then the objects property will become selected. If it’s not over the center point, it’ll be unselected.
The bounds of a collider is the bounding volume in world space. It has useful properties, such as:
- Center: The center point of the bounding box as a Vector3.
- Min: The minimal point of the box as a Vector3.
- Max: The maximum point of the box as a Vector3.
- Extents: The total distance between the center point and the edge of the box.
- Size: The total size of the box as a Vector3.
Before setting up the scene, a new script will need to be created to do three key things:
Create a new script called PlayerDetails
. Add the following logic:
public class PlayerDetail : MonoBehaviour
{
[Header("General Properties")]
public Collider MyCollider;
public GameObject SelectedHighlight;
private bool _isSelected;
public bool IsSelected
{
get => _isSelected;
set
{
//toggle whether the character is selected
_isSelected = value;
//toggle the sprite object based on selected state
SelectedHighlight.SetActive(_isSelected);
}
}
void Awake()
{
//get a reference to the collider if not set via the inspector
if (MyCollider == null)
{
MyCollider = GetComponentInChildren<Collider>();
}
//Get a reference to the sprite if not set via the inspector
if (SelectedHighlight == null)
{
SelectedHighlight = GetComponentInChildren<SpriteRenderer>(true).gameObject;
}
}
void Start()
{
//Add itself to the list of Characters which will be iterated through when detecting selection
PartyManager.Characters.Add(this);
}
}
Once again, Awake()
is being used to make sure that the script has the references needed. Start()
’s job is to add the PlayerDetails script to a static list (which will be created next). The IsSelected
property is doing the last two remaining tasks - it is toggling a bool value that represents the selected state and will toggle a visual sprite in the scene.
Next, create a new global variable that is a static List of PlayerDetails
to the PartyManager
script and instantiate it in Awake()
:
public static List<PlayerDetail> Characters;
void Awake()
{
Characters = new List<PlayerDetail>();
}
Having a static list makes it unnecessary to follow the singleton pattern or get a reference to the script for each character. This is a personal preference as the other two methods would work just fine.
It is now time to setup the Player game object:
To setup the highlight game object:
I made the witch several years ago when I was first learning to model and it appears she was exported incorrectly, so some of her properties are a bit off at times. I have not had a chance to make another player object yet. Sorry about that!
Add the PlayerDetail
script to the Player game object in the scene:
To make the test more interesting, add a couple more of the characters to the scene: