How to set rotation of instantiated object to normals direction?

Hey, I’m having some issues changing the rotation of instantiated objects (through particle collisions) so they’re flat along the surface of the object. It works on objects I create in Maya and freeze transformations, then export it into Unity but for objects created in Unity through the normal create gameobject function or with ProBuilder I get the issue seen in the image below.

The reason I want to make it work with ProBuilder objects in particular is so I can greybox and test designs rather make everything in Maya and make the workflow longer than it needs to be. I also need to make the decals wrap around objects, but that’s another issue for later (but any help with that would also be greaaat!).

Below is the code I’ve made:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SprayPaintParticles : MonoBehaviour {

    public GameObject sprayPaintGun;
    public GameObject sprayPaint;
    public Material foamMat;

    private ParticleSystem ps;
    private ParticleCollisionEvent[] collisionEvents;

    void Start()
    {
        ps = GetComponent<ParticleSystem>();
        collisionEvents = new ParticleCollisionEvent[0];
    }

    void Update()
    {
        ps.startColor = sprayPaintGun.GetComponent<SprayPaintGun>().curColor;
    }
      
    void OnParticleCollision(GameObject other)
    {
        int _collisionCount = ps.GetSafeCollisionEventSize();

        if (_collisionCount > collisionEvents.Length)
            collisionEvents = new ParticleCollisionEvent[_collisionCount];

        int _eventCount = ps.GetCollisionEvents(other, collisionEvents);

        for (int i = 0; i < _eventCount; i++)
        {
            GameObject _sprayPaintPrefab = sprayPaint;
            Vector3 _collisionPoint = collisionEvents*.intersection;*

var _normal = _sprayPaintPrefab.gameObject.transform.TransformDirection(other.gameObject.GetComponent().mesh.normals*);*
var _hitRotation = Quaternion.FromToRotation(Vector3.forward, _normal);

_sprayPaintPrefab.gameObject.GetComponent().color = sprayPaintGun.gameObject.GetComponent().curColor;
Instantiate(_sprayPaintPrefab, _collisionPoint, _hitRotation);

}

}
}
Does anyone here know how to fix this issue or where I could find out? I’ve searched google and youtube for the last day and whatever I’ve found hasn’t worked out.
Thanks for any help!

Consider the discussion on here to see what you think.

That’s an all encompassing approach that deals with “wrap around”, everything.

Someone corrected this for me from a Discord channel and now it all works! For anyone having had similar issues the fix was to change:

var _normal = _sprayPaintPrefab.gameObject.transform.TransformDirection(other.gameObject.GetComponent().mesh.normals*);*
to:
var _normal = _sprayPaintPrefab.gameObject.transform.TransformDirection(collisionEvents*.normal);*

Your article was very helpful.
Thank you.