Well basically I have a cube which when within a certain range from my first person control, starts shooting bullets. There is a problem with the raycasting used to detect when the bullets hit the player. For some reason the bullets get deleted immediately after instantiation though and I can’t figure why. This is the script of the bullet prefab used to destroy the bullets and change the HP of the player if the bullets hit him.
using UnityEngine;
using System.Collections;
public class deletion : MonoBehaviour {
public int death_counter;
public int damage;
// Use this for initialization
void Start () {
Destroy(gameObject, death_counter);
}
void Update () {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
if (Physics.Raycast(transform.position, fwd, out hit, 0.051f) )
if (hit.collider.name == "Player") {
HP health = GameObject.Find("Player").GetComponent<HP>();
health.curHp = health.curHp - damage;
}
Destroy(gameObject);
}
}
Any help would be appreciated!