C# Hit marker when collision occurs

Hello there! I’m messing around in Unity, and I wanted to make a hit marker, much like in the call of duty series, but I’m not sure how to do it.

I tried with a simple plane with a texture applied to it, but even though I set it to transparent, it had a weird transparent black background. I also tried with sprite, but I’ve never used that and I couldn’t come up with anything.

So basically, I have missiles that I shoot, and when they collide with something, I need a small picture of a hit marker to appear at the location. I was thinking of using instantiate, since I already have that with a particle effect.

Here’s my code for onCollision as is, so if someone could help me out I’d appreciate it a lot!

using UnityEngine;
using System.Collections;

public class onCollideMissile : MonoBehaviour {
public GameObject explosion;

private void OnCollisionEnter(Collision collision)
{

if ( collision.gameObject.tag == "Terrain" )
{
GameObject expl = Instantiate (explosion, transform.position, Quaternion.identity) as GameObject;
Destroy(gameObject);
Destroy(expl,0.5f);
}

}

public void destroyTimer(){
Object.Destroy(gameObject, 5.0f);
}
}

I got it to work using a sprite now, but I changed my mind. I’d rather want it to be a hitmarker on the screen, behind my model.

Right now it’s displaying this:

and as you can see, the further you get away, the smaller the hitmarker is.

So what I had in mind is like a GUI image that displays where it’s hit, relative to the place on the screen, if you understand what I mean.

Use Camera.WorldToScreenPoint to set the hitmarker’s position on your UI.

Thanks but could you elaborate a bit more?
I haven’t done any UI in unity yet (only just started unity couple days ago), so I’m not sure how I create a UI image/texture /whatever every time my missiles collide with something. Do you also use instantiate here?

Yep, pretty much. The UI works just like your regular GameObjects.

Check out the great tutorials here on the site: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

You’ll specifically want to watch UI Canvas and UI Image

This is what I got so far:

using UnityEngine;
using System.Collections;

public class onCollideMissile : MonoBehaviour {
    public GameObject hitmarker;

    public Transform target;
    Vector3 screenPos;

    public void Update (){
        screenPos = Camera.main.WorldToScreenPoint (target.position);
    }

    private void OnCollisionEnter(Collision collision)
    {

        if ( collision.gameObject.tag == "Terrain" )
        {       
            GameObject marker = Instantiate (hitmarker, screenPos, Quaternion.identity) as GameObject;
            Destroy(gameObject);
            Destroy (marker,0.2f);
        }

    }

    public void destroyTimer(){
        Object.Destroy(gameObject, 5.0f);
}
}

I’m not really sure what to put in the transform target. I tried putting in the missile object itself (the one that is colliding, creating the hit marker), but it’s not working.

If someone could point out what I’m doing wrong, please tell me =)

bump

edit: working now =)