Question by 
               ry511 · Dec 10, 2020 at 08:44 PM · 
                c#scripting problemcollidersdebugging  
              
 
              How to 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);
     }
 }
 
              
               Comment
              
 
               
              Your answer