Search Unity

Dynamic texture modification based on collisions

Discussion in 'Physics' started by jyumaidev, May 12, 2019.

  1. jyumaidev

    jyumaidev

    Joined:
    May 12, 2019
    Posts:
    2
    I want find out if there are any existing methods for modifying textures based on "directional collisions". For example, if a character gets slashed with a sword, its skin texture gets a scar texture overlayed where the sword hit, with the correct orientation. I know this is fairly simple to do for bullet holes, but what about the case I just mentioned where the orientation of the texture is important?

    I'm just looking for a starting point. I haven't gotten much on google so far. Any help would be appreciated.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    One general challenge is that in most cases, the character mesh is unlikely to closely match the collider you use for collision detection. For example, complex humanoid models are often represented as simple capsule colliders for the purpose of collision detection. But a generally simple workaround for this is to use decals for the effect (as you might already be doing with your bullet holes).

    There are some different approaches depending on how accurate you want to be. The simplest thing is probably a single OnCollisionEnter (or OnTriggerEnter, depending on your setup) for when the sword hits the collider. Take the collision's 'point', and capture the current velocity of the sword. That gives you a point and a direction. You could use that to put the scar at the right point, and adjust its rotation to more or less match the path the sword was travelling.

    Another more complex approach would be to capture the point of collision in OnCollisionEnter, and not draw the scar until you register a subsequent OnCollisionExit. That would give you two points, and you could align your scar to the direction vector those two points produce.
     
    jyumaidev likes this.
  3. jyumaidev

    jyumaidev

    Joined:
    May 12, 2019
    Posts:
    2
    This is actually quite brilliant. I did not think to use the exit collision information to get two points. Definitely going to try this.