array of raycasts in grid based game
Hi im making a battleship type of game and i need to make restrictions on where you can place your ships. I place my ships with the help of an raycast on the cursorposition called with the mousebutton. There i only needed to call 1 raycasthit. But now i want to be able to do the same but instead call an array of raycasthits surrounding the cursor.
RaycastHit[] submarineZone[];
Vector3 mousePos = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(mousePos);
for (int i = 0; i < submarineZone.Length; i++)
{
if (Physics.Raycast(ray, out submarineZone[i]))
{
if(submarineZone[i].collider.tag == "playerTile")
{
canBePlaced = true;
}
else
{
canBePlaced = false;
}
}
}
I figured it would work like this. The thing is im unsure on how i could efficiently position these raycasthits so that they would surround the cursor in a "grid-fashion". This submarine object is only 1x1 big (just a tile) and to make a correct restriction for it i would need 8 raycasthits that would surround it where it would detect if it hit something its not supposed to next to it.
I could just hard-code these positions but is there a way to define each position of the raycasthits that wouldnt eat up too many lines. OR is there a more effective way to deal with this problem that im not aware of
Thanks
Btw i havent tried this code out and right now i can see that its pretty invalid. Its not the point of the question tho and it shouldnt take too much focus
Your answer
![](https://koobas.hobune.stream/wayback/20220612075620im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
assigning several positions and call array of raycasts 0 Answers
Creating a grid that holds objects of different sizes? 0 Answers
Creating a Grid using an Array with C# 1 Answer
How can I get the click position on a Rawimage ui element? 0 Answers
RayCast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask) question 0 Answers