Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2d as damage area

Discussion in 'Scripting' started by Paykoman, Feb 28, 2020.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys, i would like to know how can i acchieve an area of damage defined by a 2d image shape that is instanteated over the ground when some attack animation is used... any tips?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    By "2d image shape" do you mean a texture where the color of the pixels defines whether they are affected or not?

    If the potential targets that could be damaged by this effect are points, then you basically need to compute which pixel of the image correspond to that point (based on where the effect was cast) and test whether that pixel is "on" or "off". (With any coordinates falling outside the bounds of the image defaulting to "off", presumably.)

    If the potential targets are areas, rather than points, then you presumably want to test whether the target area overlaps any part of the damaged area. If the damaged area is an arbitrary shape (not e.g. guaranteed to be a simple convex polygon) then every pixel needs to be tested separately, so I would guess your most efficient option is going to be to enumerate all the pixels in the image one-at-a-time, and for each pixel-enemy pair calculate whether that pixel is located inside that enemy's area. This could get expensive very quickly if your image contains a large number of pixels, so you probably want to keep it low-res if you are using this technique.

    Rather than trying to code up this algorithm yourself, you should think about representing your attack pattern as a collider and then using standard Unity collision logic to detect who gets hit by it.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I also thought of an image, but this is a double-edge sword really. I'd argue that a simple point-based system of several, maybe overlapped "effectors" can be used to approximate an irregular shape, much faster. Now whether it's a custom system or an existing type of a collider, the actual solution to be used shouldn't rely on any kind of texture, that's for sure. That's just completely unnecessary.
     
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    My question is how to acchieve a simple example, when a boss for example is casting an attack it draws on flow the area where the attack will it, couls be a circle area or a cone in front of boss. I thought that is acchieved with an image as place older for the area, am i wrong?
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    You're not wrong conceptually, but this is a bad solution because of the performance reasons. And also implementation-wise you need to manually scan this image that is transformed (mathematically computed) to a world space, and process the damage recipients etc. and practically devise an entire algorithm to do this efficiently, which is, in my mind, something only an intermediate-to-senior developer should tackle. Sorry if I'm mistaken, but I'm assuming that you're aren't well-versed with the programming of such things, since you're asking about it on a forum.
     
  6. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Yes im not. Is there a simple and easy way to handle it?
     
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Try following the last suggestion of Antistone above. That's the simplest solution I can think of, that I'm sure you can manage, simply because it is well documented here and elsewhere. You just need to nudge it a bit, to conventionally do slightly unconventional things for you.
     
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I believe you misunderstood OP. If i understood him correctly, OP does not want to use an image to define the area where you get damaged, but instead is simply asking how to draw an image (boss attack telegraph) on the ground.

    If so, you can either spawn a flat sprite in 3d space (which will clip on uneven terrain) or use a shader for it. That plus a collider to check for people who get damaged by it of course. So the visuals are not actually connected to the damage area internally. They just fill / indicate the same area. If the actual question was how to make them the same.. then you'd have to do some fancy and computational heavy calculations, for which i see no reason.
     
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    yeah well, could be, it's not that hard to misinterpret. thanks for intervening if that's the case.

    OP:
    well if that's what you ask, if you just want to achieve a visual effect of having a "blast area" you could also use a particle system instead of an image. or a combination. in that case, no need for computations and transformations, sorry if I have frightened you, though you still might run into graphical issues such as finding the correct terrain elevation and whatnot, as explained by Yoreki.
     
  10. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Yes that's what i want a simple sprite that indicates the "area damage" and if it collides with playes damage him.

    Its a game image just for demonstration, this one fill over cast time but i dont need that just a demonstrarion of the place where the damage will hit so could be avoided :p

    https://ibb.co/XpGrmDj
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357