- Home /
Problem with assigning sprites via raycast
Hi im making a battleship game and im having problem with my selection marker for the bigger ships. I have picked up a battleship (4x,1y) which i can place out on the grid and it will attach to it and when i hover over the tiles it will select with a yellow colour.
The thing is when i select a bigger ship the selection area should be of the same size (so if i pick up the cruiser of (3x,1y) i want the selection marker to increase with 2 tiles to the right of the original mouse position where there will always be selected. However i cant get any bigger than 2 selected tiles when i want to maximum 4.
Here is my raycast for tile selection for a battleship:
if(placementscript.undoBattleship)
{
gameObject.GetComponent().sprite = sprite1;
selected = true; RaycastHit TileHit1; RaycastHit TileHit2; RaycastHit TileHit3;
if(Physics.Raycast (transform.position, Vector3.right, out TileHit1, +1) && Physics.Raycast(transform.position, Vector3.right, out TileHit2,+2) && Physics.Raycast(transform.position, Vector3.right, out TileHit3,+3))
{
if(TileHit1.collider.tag == "TilePlayer" && TileHit2.collider.tag == "TilePlayer" && TileHit3.collider.tag == "TilePlayer")
{
neighbourTile1 = TileHit1.collider.gameObject;
neighbourTile2 = TileHit2.collider.gameObject;
neighbourTile3 = TileHit3.collider.gameObject;
neighbourTile1.GetComponent<SpriteRenderer>().sprite = sprite1;
neighbourTile2.GetComponent<SpriteRenderer>().sprite = sprite1;
neighbourTile3.GetComponent<SpriteRenderer>().sprite = sprite1;
}
}
}
it seems i can only acces the: neighbourTile1.GetComponent().sprite = sprite1; but the other 2 after doesnt seem to work or even wanna start. How can i go about making this work?
hey, did you end up finishing this game? I am trying to make a similar game and got stuck on how to center the battleships inside the squares.
Answer by Suddoha · Sep 11, 2015 at 01:28 PM
Your raycasts are a bit strange to me. You pass an int parameter which will either be interpreted as a layermask value or as a distance value. You also start all three raycasts from the same position (transform.position) and cast them to the right (which ever direction that may be in your setup).
With the raycast approach, you have to adjust the position from which they start for every tile.
However, i think this can be more easily and more efficiently solved with either a flat one-dimensional or a 2-dimensional array.
Your answer
![](https://koobas.hobune.stream/wayback/20220612033326im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ray from 2d image to 3d objects !!!!! 1 Answer
How to do a raycast between an UI Element and Sprite 2D? 0 Answers
Cutting a Sprite 0 Answers
Ray effect on sprite with obstacles 0 Answers
How to get pixel coordinates when mouse click on Sprite Renderer 0 Answers