I’m not sure how to post images so I’ll try to describe this as best I can.
I have a board made of hexes (not a hex grid, they are 3d objects). Some of these hexes are roads, which give a movement bonus if a unit’s movement is entirely on the road. Each hex has a “HexScript” attached which contains information on whether the hex is a road and where the road exits are (numbered 1-6 starting from the top right and going clockwise).
Movement is done by selecting a path of hexes, then saving it to do the movement later.
I have this code to check whether the next hex being selected is joined to the last hex by road:
if (hex.isRoad && RoadCheck(destination, lastHex))
{
roadBonus = mover.roadBonus;
}
else
{
roadBonus = 0;
print(hex.isRoad);
print(RoadCheck(destination, lastHex));
}
which calls
bool RoadCheck(GameObject hex, GameObject lastHex)
{
bool returnValue;
Vector3 otherRelativePos = hex.transform.position - lastHex.transform.position;
int value = Mathf.RoundToInt(Mathf.Rad2Deg * Mathf.Acos(Vector3.Dot(lastHex.transform.forward, otherRelativePos) / (otherRelativePos.magnitude * hex.transform.forward.magnitude)));
switch (value)
{
case 30:
returnValue = RoadFinalCheck(hex, 4);
print(30);
break;
case 90:
returnValue = RoadFinalCheck(hex, 5);
print(90);
break;
case 150:
returnValue = RoadFinalCheck(hex, 6);
print(150);
break;
case 210:
returnValue = RoadFinalCheck(hex, 1);
print(210);
break;
case 270:
returnValue = RoadFinalCheck(hex, 2);
print(270);
break;
case 330:
returnValue = RoadFinalCheck(hex, 3);
print(330);
break;
default:
print("error: case is " + value);
returnValue = false;
break;
}
return returnValue;
}
which calls
bool RoadFinalCheck(GameObject hex, int num)
{
return hex.GetComponent<HexScript>().roadExits.Contains(num);
}
This works fine for regular roads, but another terrain type, town, acts as roads with exits in every direction. I started having a problem in one specific situation where moving from a town to a normal road would not register as moving along a road (it works when moving the other way though), so I threw in some print() statements to figure out what was going on.
I found that the switch in RoadCheck() is printing 90 every single time, meaning the selected hex is on the right of the last hex, even when it is clearly not, but it still works in every situation (except that one I found) with no issues and I have no idea why.
Please help I am going mad. Let me know what other info you need.