Bullet hole random rotation and size

Hi!
I’m new to the Unity and I’m learning how to make a basic FPS. Right now, I’m stuck at making bullet holes have random rotation along the normal.

My code:

public float Cooldown = 0.1f;
float CooldownRemaining = 0;
public float Range = 100.0f;
public float BulletHoleOffset = 1.0f;

public GameObject debrisPrefab;
public GameObject BulletHole;

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {
    CooldownRemaining -= Time.deltaTime;
    if (Input.GetMouseButton(0) && CooldownRemaining <= 0)
    {
        CooldownRemaining = Cooldown;
        Ray ray = new Ray(Camera.main.transform.position + Camera.main.transform.forward, Camera.main.transform.forward);
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit, Range))
        {
            Vector3 hitPoint = hit.point;

            if (debrisPrefab != null)
            {
                //Particle effect
                Instantiate(debrisPrefab, hitPoint, Quaternion.FromToRotation(Vector3.up, hit.normal));
                //Bullet Hole
                Instantiate(BulletHole, hitPoint + (hit.normal * BulletHoleOffset), Quaternion.FromToRotation(Vector3.forward, hit.normal));
            }
        }
    }	
}

I’m stuck at “Quaternion.FromToRotation(Vector3.forward, hit.normal));” since I don’t know where to “plug” random rotation along the normal.
I managed to get a random rotation along particular axis, but it didn’t look good since it didn’t align with the normal.

Similar to above. How to achive random hole size?

Thanks in advance!

Use Transform.Rotate, note that oyu can pass a parameter that tells the method to use local space (which is always the default value if you don’t pass the parameter). Rotate random degrees around the local y-axis and you are set :slight_smile:
For the random scale, just pass a Vector3 composed of random values to the bullet Transform’s localScale variable.

//Bullet Hole
GameObject bulletHole =  Instantiate(BulletHole, hitPoint + (hit.normal * BulletHoleOffset), Quaternion.FromToRotation(Vector3.forward, hit.normal));
float randomRot = Random.Range(0f, 360f);
bulletHole.transform.Rotate(bulletHole.transform.right * randomRot);
float randomScale = Random.Range(0.5f, 1.5f);
bulletHole.transform.localScale = new Vector3(randomScale ,randomScale ,randomScale);

EDIT: CORRECTED CODE & SOLUTION (See comments below);

public float Cooldown = 0.1f;
      float CooldownRemaining = 0;
      public float Range = 100.0f;
      public float BulletHoleOffset = 1.0f;
      public GameObject debrisPrefab;
      public GameObject BulletHole;
      // Use this for initialization
      void Start () {
          
      }
      
      // Update is called once per frame
      void Update () {
          CooldownRemaining -= Time.deltaTime;
          if (Input.GetMouseButton(0) && CooldownRemaining <= 0)
          {
              CooldownRemaining = Cooldown;
              Ray ray = new Ray(Camera.main.transform.position + Camera.main.transform.forward, Camera.main.transform.forward);
              RaycastHit hit;
              if(Physics.Raycast(ray, out hit, Range))
              {
                  Vector3 hitPoint = hit.point;
                  if (debrisPrefab != null)
                  {
                      //Particle effect
                      Instantiate(debrisPrefab, hitPoint, Quaternion.FromToRotation(Vector3.up, hit.normal));
                      //Bullet Hole
                      GameObject bulletHole =  Instantiate(BulletHole, hitPoint + (hit.normal * BulletHoleOffset), Quaternion.identity) as GameObject;
       bulletHole.transform.rotation = Quaternion.FromToRotation(bulletHole.transform.up, hit.normal) * bulletHole.transform.rotation;
  float randomRot = Random.Range(0f, 360f);
       bulletHole.transform.Rotate(0, randomRot, 0);
       float randomScale = Random.Range(0.5f, 1.5f);
       bulletHole.transform.localScale = new Vector3(randomScale ,randomScale ,randomScale);
                  }
              }
          }    
      }