-
December 30th, 2021, 11:51 PM
#1
Set to coordinates to centre of grid
I have these buttons created in unity- 27 by 27 which has 9 by 9 subgrids Each button is 10 by 15 pixels. I want to get the coordinates of the centre of each subgrid(which has no grouping) which are numbered 1 to 9. There must be a neater way than coding
Code:
private int _num
private int devy
private int devx
private float _Xcood
private float _Ycood
if (_num==1 OR _num==2 OR _num==3)
{ devy =1}
else if (_num==4 OR _num==5 OR _num==6)
{devy=0}
else if (_num==7 OR _num==8 OR _num==9)
{devy=-1}
if (_num==3 OR _num==6 OR _num==9)
{ devx =1}
else if (_num==2 OR _num==5 OR _num==8)
{devx=0}
else if (_num==1 OR _num==4 OR _num==7)
{devx=-1}
_Xcood=devx*10+position.y
_Ycood=dexv*15+position.x
Last edited by 2kaud; January 1st, 2022 at 05:30 AM.
Reason: Added code tags
-
December 31st, 2021, 01:18 PM
#2
Re: Set to coordinates to centre of grid
Originally Posted by cloa513
There must be a neater way
I do not know what you mean by neater, but this is another way of assigning values to devy and devx,
Code:
if (_num <= 3) {
devy = 1;
devx = _num - 2;
} else if (_num <= 6) {
devy = 0;
devx = _num - 5;
} else {
devy = -1;
devx = _num - 8;
}
-
December 31st, 2021, 05:59 PM
#3
Re: Set to coordinates to centre of grid
Originally Posted by wolle
I do not know what you mean by neater, but this is another way of assigning values to devy and devx,
Code:
if (_num <= 3) {
devy = 1;
devx = _num - 2;
} else if (_num <= 6) {
devy = 0;
devx = _num - 5;
} else {
devy = -1;
devx = _num - 8;
}
More efficient- look how lines are needed and I doubt your improved code (while better than mine) fundamentally ensures the right result.
I run mine with correct syntax and the location of the new object is all over the place - it should never overlap but they do.
-
January 4th, 2022, 04:44 AM
#4
Re: Set to coordinates to centre of grid
Originally Posted by cloa513
I doubt your improved code (while better than mine) fundamentally ensures the right result.
My code is logically the same as yours. If your code is wrong, so is mine. Your problem description indicates you have a working solution and now want help making your code become "neater".
Last edited by wolle; January 7th, 2022 at 03:46 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|