Hi, i have a script that does raycast bullets. I want it so that when it hits a player (that will be tagged) it will get the heath script and call a function that mkes it lose health.
here is my shooting script:
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
//public GameObject bullet;
//public GameObject muzzleflash;
public GameObject bulletHole;
public GameObject spark;
public GameObject dirt;
public float delayTime = 0.5f;
private float counter = 0;
void FixedUpdate ()
{
if(Input.GetKey(KeyCode.Mouse0) && counter > delayTime)
{
//Instantiate(bullet, transform.position, transform.rotation);
//Instantiate(muzzleflash, transform.position, transform.rotation);
GetComponent<AudioSource>().Play();
counter = 0;
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit, 100f))
{
Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
Instantiate(spark, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
Instantiate(dirt, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
}
counter += Time.deltaTime;
}
}
and here is my health script (very simple)
using UnityEngine;
using System.Collections;
public class health : MonoBehaviour {
public float damage = 10f;
public float totalhealth = 250f;
// Use this for initialization
void Start () {
}
public void loselife(){
if (totalhealth > 0) {
totalhealth = totalhealth - damage;
} else {
Destroy (gameObject);
}
}
// Update is called once per frame
void Update () {
}
}