Im new to unity and im following brackeys tutorial on shooting with raycast and ive ran into an issue with the crate not being destroyed after 5shots. I really need this solved because breaking the objects and shattering objects will be an extreme feature in my game. I dont know what script is the problem.
Target script:
using UnityEngine;
public class Target : MonoBehaviour
{
public float health = 50f;
public void TakeDamage(float amount)
{
health -= amount;
if (health <= 0f)
{
Die();
}
}
void Die()
{
Destroy(gameObject);
}
}
Gun script
using UnityEngine;
public class GUN : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown(“Fire1”))
{
Shoot();
}
}
void Shoot ()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent();
if (target != null)
{
target.TakeDamage(damage);
}
}
}
}