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);
}
}
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?
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 =)