Hello,
a few weeks ago i made a shooting script that uses a raycast for shooting. Currently it spawn a particle prefab on hit but i want it to decrease the health of that object (if it has the tag “Enemy”)
These are the scripts i have:
shooting:
using UnityEngine;
using System.Collections;
public class GunHit
{
public float damage;
public RaycastHit raycastHit;
}
public class RaycastGun : MonoBehaviour
{
public float fireDelay = 0.1f;
public float damage = 1.0f;
public string buttonName = "Fire1";
public LayerMask layerMask = -1;
private bool readyToFire = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown (buttonName) && readyToFire)
{
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.forward,out hit, Mathf.Infinity,layerMask))
{
GunHit gunHit = new GunHit();
gunHit.damage = damage;
gunHit.raycastHit = hit;
hit.collider.SendMessage("Damage",gunHit, SendMessageOptions.DontRequireReceiver);
}
}
}
}
and a simple health script:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public float maxHealth = 100f;
public float currentHealth = 100f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
The fireDelay doesnt do anything yet but i am still working on that.
Thanks,
me