I am using this script Gravity and am trying to change this line
Collider[] cols = Physics.OverlapSphere(transform.position, range);
to use an attached game object to get the objects within the collision area. How would I change it? The idea is to be able to apply the script to an item, box, sphere, mesh, whatnot, the give it a game object that surrounds that object (like atmosphere around a planet) and pull anything inside that shape towards the center.
Try this…
cols = Physics.OverlapSphere(transform.position, range);
List <GameObject> _objects = new List<GameObject>();
foreach(Collider c in cols) {
GameObject go = c.gameObject;
_objects.Add(go);
}
_objects should contain all of the GameObjects you wanted to detect.
There is no irregular shaped Physics cast but all you need is the distance anyway. You could create a grid of flattened box colliders on a layer not colliding with your GameObjects outlining the shape you want to monitor then raycast towards objects detected in a spherecast with a radius of the farthest point in the grid from center. if the ray gets out of your grid and hits the object, you know it’s inside the polygonal shape formed by the grid.