Unity 2D bullet bounce
I am trying to make a 2d top down shooting game, but instead of the bullet disappearing when it hit the wall i want it to bounce off of the wall. Then when it hits the enemy to do damage and disappear.
This is my health code:
using UnityEngine;
using System.Collections;
public class DamageHandler : MonoBehaviour {
public int health = 1;
public float invulnPeriod = 0;
float invulnTimer = 0;
int correctLayer;
SpriteRenderer spriteRend;
void Start() {
correctLayer = gameObject.layer;
// NOTE! This only get the renderer on the parent object.
// In other words, it doesn't work for children. I.E. "enemy01"
spriteRend = GetComponent<SpriteRenderer>();
if(spriteRend == null) {
spriteRend = transform.GetComponentInChildren<SpriteRenderer>();
if(spriteRend==null) {
Debug.LogError("Object '"+gameObject.name+"' has no sprite renderer.");
}
}
}
void OnTriggerEnter2D() {
health--;
if(invulnPeriod > 0) {
invulnTimer = invulnPeriod;
gameObject.layer = 10;
}
}
void Update() {
if(invulnTimer > 0) {
invulnTimer -= Time.deltaTime;
if(invulnTimer <= 0) {
gameObject.layer = correctLayer;
if(spriteRend != null) {
spriteRend.enabled = true;
}
}
else {
if(spriteRend != null) {
spriteRend.enabled = !spriteRend.enabled;
}
}
}
if(health <= 0) {
Die();
}
}
void Die() {
Destroy(gameObject);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612064105im_/https://answers.unity.com/themes/thub/images/avi.jpg)