- Home /
How to use correctly a SphereCastAll
I'm trying to create a tactical-turn-based-tile-game. I need to be able to check all the available points to where my character can move in a radius of 2 for exemple. I was thinking about using SphereCastAll to get all points around the unit doing the following:
RaycastHit[]FoundWaypoints = Physics.SphereCastAll(Character.transform.position, 2.0f, Vector3.forward);
But this doesn't quite do what I expect it to do so I think I'm getting all wrong how SphereCast is used. What I except that is happening is a sphere that gets build around Character position in a radius of 2, but that's not really what is happening. Does anyone have an idea what I could do to make this work?
Sorry if it isn't so clear I'm not that good in english. Tell me if something isn't clear.
Answer by Deign · May 10, 2014 at 05:51 PM
Spherecast is used the same as Raycast, the only difference is the diameter of the raycast. What I think you're actually trying to do is OverlapSphere
https://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html
This was exactly what I needed cheers. Just going to try and figure out now how I could cut the corners.... that's gonna be a bit harder ... ^^
Thank you so much. Physics.OverlapSphere is very much useful for me to detect multiple object within specific radius. Thanks a lot.
Answer by ttRevan · Oct 30, 2015 at 08:51 PM
IMHO you don't need Physics at all. Simple tiling cordiates can solve your problem:
------ ----- ----- -----
| 1.1 | 1.2 | 1.3 | 1.4 |
----- ----- ----- -----
| 2.1 | 2.2 | x | 2.4 |
----- ----- ----- -----
| 3.1 | 3.2 | 3.3 | 3.4 |
----- ----- ----- -----
You can then find matching tiles:
var xNum = Mathf.Ceil(range / tileSizeX);
var yNum = Mathf.Ceil(range / tileSizeY);
List<Tile> availPoints = new List<Tile>();
for (var i = 1; i <= xNum; i++) {
availPoints.Add(allTiles.Get(playerTile.x + i, playerTile.y));
availPoints.Add(allTiles.Get(playerTile.x - i, playerTile.y));
}
for (var i = 1; i <= yNum; i++) {
availPoints.Add(allTiles.Get(playerTile.x, playerTile.y + i));
availPoints.Add(allTiles.Get(playerTile.x, playerTile.y - i));
}
In case you have hex tiles, algorithm will be somewhat different, but idea is the same.