0
\$\begingroup\$

I have a playing field which is divided in 4 zones (as seen on the image below)

Playing field 2

The player (blue) is on the second zone while there are 2 enemies (red) on the third zone. When the player throws an object and hits the enemy, the enemy is knockbacked in the next zone (4) but should stay within that zone.

If the enemy was being hit on the edge of the zone, I want him to still be in the zone 4 and not outside of it (if he got knocked back). I'm not sure if the direction of the hit matters in the calculation

Question: How can I find X?

Note that it can be on any shape of playing field (triangular, hexagonal, circular, ...)

\$\endgroup\$
3
  • \$\begingroup\$ Can you unpack that "according to the direction of the hit" in more detail? It looks like both X marks in your example moved straight leftward, even though they were hit from different directions. So what influence should the hit direction have, in other cases? \$\endgroup\$ – DMGregory Jan 26 '20 at 22:08
  • \$\begingroup\$ Sure to elaborate a bit more: if the enemy was more to the top of the circle (Y increase) and was being hit, I want him to still be in the zone 4 and not outside of it (if he got knocked back). I'm not sure if the direction of the hit matters in the calculation. \$\endgroup\$ – XverhelstX Jan 26 '20 at 23:21
  • \$\begingroup\$ I've added a bit more context to the question \$\endgroup\$ – XverhelstX Jan 27 '20 at 9:01
0
\$\begingroup\$

Looks like a job for the Physics engine. I would give the boundary of your play area a Rigidbody, and when the enemy is hit you apply a force to their Rigidbody equivalent to the vector (force) of the projectile (or similar). The physics engine will try to move that enemy away, in the same direction as the projectile, but if they hit the boundary wall they would 'slide' round it.

\$\endgroup\$
2
  • \$\begingroup\$ The boundary needs a collider, but not a Rigidbody, unless you plan to move the play field borders in real-time. \$\endgroup\$ – DMGregory Jan 30 '20 at 14:01
  • \$\begingroup\$ True... But only if the units have Rigidbodies. There's got to be an RB in there somewhere ;o) \$\endgroup\$ – allnodcoms Jan 30 '20 at 14:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.