Creating decal, terrain normals not working

I am creating bullet hole decals on objects when I shoot them. This works on everything except for terrain where the decals will sometimes hover over the terrain and never have the correct rotation (always pointing up). Here's the code:

    Vector3 hitPosition = new Vector3
    {
        x = collision.contacts[0].point.x,
        y = collision.contacts[0].point.y,
        z = collision.contacts[0].point.z
    };

    Quaternion rotation = Quaternion.FromToRotation(Vector3.up, collision.contacts[0].normal);

    //Create the decal (ie: bullet hole)
    GameObject decal = (GameObject)Instantiate(DecalPrefab, hitPosition, rotation);

1 Answer

1

I think you should use Quaternion.LookRotation rather, and note that the order is reversed if you want to supply the up vector. The up vector come as second argument. Does this do the trick?

ContactPoint contact = collision.contacts[0];
Quaternion rotation = Quaternion.LookRotation(contact.normal);
GameObject decal = (GameObject)Instantiate(DecalPrefab, contact.point, rotation);