I’m using physical bullets in my prototype for an arcadey air combat game. I was trying to use the bullet’s own impact scripts to set the hitmarker on my ui layer to setactive when they collide with a target and didn’t have any luck, so now I want to try making a script on the hitmarker gameobject itself that detects if a bullet has collided with a target. However I’m very new to coding and I’m having trouble finding resources for either method. Can anyone point me in the right direction?Z
This is all I have so far:
public class HitMarkerScript : MonoBehaviour
{
public GameObject bullet;
public GameObject hitMarker;
public AudioClip hitMarkerSound;
// Start is called before the first frame update
void Start()
{
hitMarker.SetActive(false);
bullet.GetComponent<Collider>();
}
I did see one video saying you can use a lerp on the images transparency to fade the marker in and out, but I’d like to get the basic functionality first. Where do I go from here?
If I understood correctly you want to detect collisions between bullets and targets, and then activate some hitmarker UI element based on the result.
For this job I think that the OnCollisionEnter or OnTriggerEnter function are perfect.
For example you could do something like
// This method is called when the collider associated to this object collides with another collider
void OnCollisionEnter(Collision collision)
{
// You can check collision objects based on tags or if you want something fancier you can use a LayerMask
if (collision.other.CompareTag("Target"))
{
ActivateHitMarker();
}
}
It s up to you to decide which object should manage the collision
However when I assigned my hitmarker prefab to the hitMarker gameobject, it didnt show up on my UI so I’m not sure how to call it correctly from the bullet script. That’s why I thought to try a script directly on the Hitmarker UI that would get the bullet’s collider and then check it for collision. But if I can do it directly from the bullet that will probably be less buggy for reuse and perform a little better
Probably best to start with some simple tutorials on using OnCollisionEnter().
I say this simply because this code:
accomplishes nothing. The above code can be deleted.
If you want to do something with the “gotten” Collider, assign it to a variable.
Otherwise, if you want examples of colliding bullets against stuff, check out any space shooter tutorial, or you’re welcome to see some of the micro-game examples in my Proximity Buttons package, such as the DemoSpaceShooter and DemoTwinStickShooter scenes… I forget how I did the collision but I think it was just coordinate checks to keep it simple and fast.
proximity_buttons is presently hosted at these locations:
I already have bullet collision and damage, which I used onCollisionEnter to implement. I just need a way to use that collision as a trigger to make the hitmarker ui element appear. I put bullet.GetComponent(); because I figured I would need to do some kind of check on that collider next to see if it’s collided with something damageable