I'm using a scroll view to store my game character icons (basically an RPG). The movement system for the characters is tile based, but in game hazards could be located anywhere. So I need to detect if a certain tile will interset with a hazard if a character moves into it. To do this I can calculate the corners of the tile using the co-ordinate system of the scroll view. I thought I could turn these into world co-ordinates then query to see if the collider attached to the hazard contains the point (the collider could be rotated so I can't just do the math myself).

When I try to convert the co-ordinates into world co-ordinates though the numbers I get just don't match up. For example, the lower left corner of the tile a character will move into is at (-1250.0, -938.0). If I convert that to world co-ordinates I get (-28.7, -49.8). The actual position for the hazard I'm testing against has a world position of (-8.0, -1.5, 93.4) though and the test tile should be right on top of it.

I'm using code like this to try to get the world co-ordinates:

  Vector2 charPosition5 = travellingCharacter.owningGameObject.GetComponent<RectTransform>().transform.TransformPoint(travellingCharacter.GetComponent<RectTransform>().anchorMax);
  Vector2 charPosition6 = travellingCharacter.GetComponent<RectTransform>().transform.TransformPoint(travellingCharacter.owningGameObject.GetComponent<RectTransform>().anchorMin);

Can someone tell me what values I should be feeding in or what other function I should be using please?

  • \$\begingroup\$ "I'm using a scroll view to store my game character icons (basically an RPG)" This doesn't sound like the best way to solve this problem, and I think it's likely to lead to more complexity than you need. Can you explain why you're using a scrollview for this purpose? \$\endgroup\$ – DMGregory Dec 29 '18 at 18:31
  • \$\begingroup\$ I want any given battle map to be larger than the screen. So I figured putting a large background image on a scroll view then placing the chararcters and game hazards on top of it would be the simplest option. \$\endgroup\$ – Allan Mills Dec 29 '18 at 18:46
  • \$\begingroup\$ This is normally handled by just having a camera that views part of the game world at a time. You'll likely find setting it up in a more conventional way like this will save you a lot of grief. \$\endgroup\$ – DMGregory Dec 29 '18 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.