Bullet Impact Effect Not Parenting to Enemy Object.

Hey guys, hope everyone is doing well.

I’ve noticed that whenever I leave an impact effect it does not stick to my enemies. It sort of looks like its stuck in space and doesn’t follow the object as its pushed back.

Can someone please explain to me what can be done in order to have my impact effect stick on the surface of objects it collides with?

Thanks again for any insight that is given.

Below is my projectile code.

using UnityEngine;
using System.Collections;

public class Projectile : MonoBehaviour
{
    public GameObject impactPrefab;

    public Rigidbody thisRigidbody;

    private Collider thisCollider;

    public int damage = 10;

    public bool LookRotation = true;

    private bool Missile = false;

    private bool explodeOnTimer = false;

    float timer;

    private Vector3 previousPosition;

    void Start()
    {
        thisRigidbody = GetComponent<Rigidbody>();

        thisCollider = GetComponent<Collider>();

        previousPosition = transform.position;
    }

    void FixedUpdate()
    {
        if (LookRotation && timer >= 0.05f)
        {
            transform.rotation = Quaternion.LookRotation(thisRigidbody.velocity);
        }

        CheckCollision(previousPosition);

        previousPosition = transform.position;
    }

    void CheckCollision(Vector3 prevPos)
    {
        RaycastHit hit;

        Vector3 direction = transform.position - prevPos;

        Ray ray = new Ray(prevPos, direction);

        float dist = Vector3.Distance(transform.position, prevPos);

        if (Physics.Raycast(ray, out hit, dist))
        {
            transform.position = hit.point;

            Quaternion rot = Quaternion.FromToRotation(Vector3.forward, hit.normal);

            Vector3 pos = hit.point;

            Instantiate(impactPrefab, pos, rot);

            if (!explodeOnTimer && Missile == false)
            {
                Destroy(gameObject);
            }

            else if (Missile == true)
            {
                thisCollider.enabled = false;

                thisRigidbody.velocity = Vector3.zero;

                Destroy(gameObject, 5);
            }

        }
    }

    void OnCollisionEnter(Collision collision)
    {

        IDamageable damageable = collision.gameObject.GetComponent<IDamageable>();

        if (damageable != null)
        {
            damageable.DealDamage(damage);
        }

        if (collision.gameObject.tag != "FX")
        {
            ContactPoint contact = collision.contacts[0];

            Quaternion rot = Quaternion.FromToRotation(Vector3.forward, contact.normal);

            Vector3 pos = contact.point;

            Instantiate(impactPrefab, pos, rot);

            if (!explodeOnTimer && Missile == false)
            {
                Destroy(gameObject);
            }

            else if (Missile == true)
            {

                thisCollider.enabled = false;

                thisRigidbody.velocity = Vector3.zero;

                Destroy(gameObject, 5);

            }
        }
    }
}

You can parent the object being instantiated to the object that was collided with. Something like this should work.

var impactEffect = Instantiate(impactPrefab, pos, rot);
impactEffect.transform.SetParent(transform, true);

Bonus tip! Use gameObject.CompareTag(“SomeTag”) rather than gameObject.tag == “SomeTag” if you can help it. It’s ever so slightly faster and doesn’t generate garbage.

1 Like

Where would this code go? I’d assume the enemy itself but I am not sure.

This code you gave me looks like Java but again i’m novice and could be wrong.

Thanks again!

I’ve been researching all night and find little bits of info on this matter. Can anyone guide me to a good Article on Parenting?

I am not sure what to do with the previous code given to me in this thread.

Java and C# are almost exactly the same syntactically, though “var” is a feature in C#, but not in Java (you may be thinking of JavaScript instead).

Anyways, you would place this code wherever you handle your bullet object colliding with an enemy. I’m assuming that’s in this section here:

void OnCollisionEnter(Collision collision)
    {
        //etc...

        if (collision.tag != "FX")
        {
            ContactPoint contact = collision.contacts[0];
            Quaternion rot = Quaternion.FromToRotation(Vector3.forward, contact.normal);
            Vector3 pos = contact.point;

            var impactEffect = Instantiate(impactPrefab, pos, rot);
            impactEffect.transform.SetParent(collision.transform, true);
        }

        //etc...
     }

What the code does is make your impact prefab a child object of the enemy GameObject after instantiating it, thus making it “stick” to the enemy by following its position and rotation.
It’s the runtime equivalent of dragging a GameObject onto another GameObject in the hierarchy window.

Here’s a video explaining a bit about it:

https://www.youtube.com/watch?v=9rR3AS74UH0

1 Like

Thanks for the help friend.

The video was perfect btw.

I’ll try editing the code tonight after I get home from work!

It worked! Thank you so much for explaining it in detail.

What I had been doing incorrectly was trying to place the code in Line 63.

I understand now that where you placed it is correct because that where the collision is called.