Place a sprite on the face of a collider

Hey guys,

My goal is to make an object look dirty by adding a dirt image to each face of an object.

As far as I can figure it should all be working well but sprites are still showing up inside the objects mesh. I have tried mathing out how far off they are to see if I am missing something but I cant piece together what is wrong.

Any advice or help is much appreciated. Thanks in advance!

public class DirtManager : MonoBehaviour
{
public GameObject dirtPrefab;
public Piece piece;
public float dirtSpread;
public float distance;
List<List<GameObject>> images;

// Start is called before the first frame update
void Start()
{
images = new List<List<GameObject>>();
BoxCollider col = gameObject.transform.parent.GetComponentInChildren<BoxCollider>();
gameObject.transform.parent = col.transform;

// grab needed info about collider
Vector3 cent = col.center;
Vector3 exts = col.size / 2;

// Spawn a piece of dirt for each face.
// Pass in the center of the collider, the vector that points to the center of the speficif face, and the dimensions of the face
SpawnDirtForFace(cent, Vector3.back * exts.z, new Vector3(exts.x, exts.y, 0));
SpawnDirtForFace(cent, Vector3.forward * exts.z, new Vector3(exts.x, exts.y, 0));

SpawnDirtForFace(cent, Vector3.left * exts.x, new Vector3(0, exts.y, exts.z));
SpawnDirtForFace(cent, Vector3.right * exts.x, new Vector3(0, exts.y, exts.z));

SpawnDirtForFace(cent, Vector3.up * exts.y, new Vector3(exts.x, 0, exts.z));
SpawnDirtForFace(cent, Vector3.down * exts.y, new Vector3(exts.x, 0, exts.z));
}

private void SpawnDirtForFace(Vector3 center, Vector3 face, Vector3 faceDims)
{
List<GameObject> objs = new List<GameObject>();

for (int i = 0; i < Mathf.CeilToInt(piece.dirtyness); i++)
{
GameObject dirt = Instantiate<GameObject>(dirtPrefab, this.transform);
// Set the piece of dirts postion to the center + face and a little extra so the image isnt hidden by objects mesh
// Also move the dirt randomly within the face of the object
Vector3 pos = (center + face) * 1.01f + new Vector3(Random.Range(-faceDims.x, faceDims.x), Random.Range(-faceDims.y, faceDims.y), Random.Range(-faceDims.z, faceDims.z));
dirt.transform.localPosition = pos;

// Make the dirt image face out from the center of the object
if (face.x != 0)
{
dirt.transform.rotation = Quaternion.LookRotation(dirt.transform.parent.transform.right, dirt.transform.parent.transform.up);
}
else if (face.y != 0)
{
dirt.transform.rotation = Quaternion.LookRotation(dirt.transform.parent.transform.up, dirt.transform.parent.transform.forward);
}
else if (face.z != 0)
{
dirt.transform.rotation = Quaternion.LookRotation(dirt.transform.parent.transform.forward, dirt.transform.parent.transform.right);
}

objs.Add(dirt);
}

images.Add(objs);
}
}

The default renderer on a sprite sorts differently than the standard renderer. Is that your problem?

To see what I mean, drag a sprite into a scene, then create a default cube nearby it, and view them from all angles around a compass. You’ll see what I mean.

If so, you need to use a diffuse cutout shader on the sprite, or something else that uses Z-sorting for draw ordering.

The problem isn’t with not being able to see the images. Its that the images are not going to the edge of the collider.

In this picture for example it says this collider is 0.3590834 along the z direction, and the center is at 0.04753689 z. My image is being placed at 0.2729034 z but would need to be at around 0.5716 z to be outside the collider.

6609937--752116--upload_2020-12-10_15-4-41.png

Is it because the collider is marked convex? That collider looks almost like a box or cube…

Nope, its a regular box collider.

Thanks for taking a look by the way! Much appreciated.