0
\$\begingroup\$

EDIT: As of yet I have not been able to find a way to make the bullet hole wrap around the corner of a gameobject.

I am working on a fps game that uses raycast for shooting and so far it works, but the problem is with the bullet holes. So the script will generate bullet holes (not going to explain everything it does) like it is supposed to.

The problem is that if I shoot the corner the bullet hole will just generate on whatever side the impact was closest to. How do I get the bullet hole to spawn on curves and corners? Here is the script:

public class HitFXManager : MonoBehaviour
{

    [Range(1, 200)]
    public int decalsPoolSizeForEachType;

    public GameObject concreteDecal;
    public GameObject woodDecal;
    public GameObject dirtDecal;
    public GameObject metalDecal;

    [HideInInspector]
    public GameObject[] concreteDecal_pool;
    [HideInInspector]
    public GameObject[] woodDecal_pool;
    [HideInInspector]
    public GameObject[] dirtDecal_pool;
    [HideInInspector]
    public GameObject[] metalDecal_pool;

    void Start()
    {
        DecalsPool();
    }

    public void DecalsPool()
    {
        concreteDecal_pool = new GameObject[decalsPoolSizeForEachType];
        var decalsParentObject_concrete = new GameObject("decalsPool_concrete");

        for (int i = 0; i < decalsPoolSizeForEachType; i++)
        {
            concreteDecal_pool[i] = Instantiate(concreteDecal);
            concreteDecal_pool[i].SetActive(false);
            concreteDecal_pool[i].transform.parent = decalsParentObject_concrete.transform;
        }

        woodDecal_pool = new GameObject[decalsPoolSizeForEachType];
        var decalsParentObject_wood = new GameObject("decalsPool_wood");

        for (int i = 0; i < decalsPoolSizeForEachType; i++)
        {
            woodDecal_pool[i] = Instantiate(woodDecal);
            woodDecal_pool[i].SetActive(false);
            woodDecal_pool[i].transform.parent = decalsParentObject_wood.transform;
        }

        dirtDecal_pool = new GameObject[decalsPoolSizeForEachType];
        var decalsParentObject_dirt = new GameObject("decalsPool_dirt");

        for (int i = 0; i < decalsPoolSizeForEachType; i++)
        {
            dirtDecal_pool[i] = Instantiate(dirtDecal);
            dirtDecal_pool[i].SetActive(false);
            dirtDecal_pool[i].transform.parent = decalsParentObject_dirt.transform;
        }

        metalDecal_pool = new GameObject[decalsPoolSizeForEachType];
        var decalsParentObject_metal = new GameObject("decalsPool_metal");

        for (int i = 0; i < decalsPoolSizeForEachType; i++)
        {
            metalDecal_pool[i] = Instantiate(metalDecal);
            metalDecal_pool[i].SetActive(false);
            metalDecal_pool[i].transform.parent = decalsParentObject_metal.transform;
        }

    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Is this really a raycasting problem, or just a decal-ing problem? ie. Do you need help detecting the hit point on the curved surface? Or just with stamping a known hit point with a hole decal that fits the curve? \$\endgroup\$ – DMGregory Jul 29 '19 at 22:32
  • \$\begingroup\$ @DMGregory You are right I will take the raycasting tag off. I updated my question and put my script in. \$\endgroup\$ – Ethan K-B Jul 29 '19 at 23:09
  • \$\begingroup\$ Presumably you considered using deferred decals for this? \$\endgroup\$ – DMGregory Jul 29 '19 at 23:21
  • \$\begingroup\$ @DMGregory Are you talking about the $5 asset in the asset store? \$\endgroup\$ – Ethan K-B Jul 29 '19 at 23:35
  • \$\begingroup\$ There are many assets that implement this technique, or you could roll your own if you prefer. \$\endgroup\$ – DMGregory Jul 29 '19 at 23:35
1
\$\begingroup\$

You need the raycast to continue past the initial point of contact. Use RaycastAll for this. Then test if you hit your target object, however you’d like to test for that. Then, if you detect a hit, and if the next RaycastHit is also the target object and is within float maxBulletPenetration dist, just get the surface normal and hit.point from both hits and spawn your decals.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.