- Home /
Decreasing health on (raycast)hit
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
Answer by Hexer · Jul 26, 2015 at 06:09 PM
That is pretty easy, first make sure to set currentHealth as a static variable, with this you can access it from within another script. We will need to access this later on. The method is (ScriptName.variable)
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
// Use this for initialization
public float maxHealth = 100.0f;
static public float currentHealth = 100.0f;
void Start () {
}
}
When the player got hit you put this in the statement. This will change the value of currentHealth to a new value.
//Health.currentHealth will be the currenthealth left minus the damage you received
Health.currentHealth = Health.currentHealth-damage;
Debug.Log (Health.currentHealth);
Wich part of my raycastgun script should i replace with the second script and what should i keep? I don't know what i need to keep to make it check if it hits the player and decreases its life.
@robbie696 please post non-answers as comments, thanks,
if (Physics.Raycast (transform.position, transform.forward,out hit, $$anonymous$$athf.Infinity,layer$$anonymous$$ask))
{
}
I guess this is the line of code that checks if it hit the player with raycast. Put the seoncd script inbetween that if-statement. You don't need to change anything from your own script if im right.
It now does get damage, but it only takes away 1,0 from the max/currentHealth. I have changed the "damage" to 25.0f but it only goes down with 1 health per shot.
gunHit.damage = damage;
Remove this, it is not needed.
public float damage = 1.0f;
Change this to :
public float damage;
Now change the damage in the inspector and try again. When you want to pick different weapons with different damage output then I should recommend you to watch a Unity Tutorial about Switch-Statements. https://unity3d.com/learn/tutorials/modules/beginner/scripting/switch
Your answer
Follow this Question
Related Questions
Unity5 WebGl for Mobile Browsers? 7 Answers
Creating a Random Walking Script 1 Answer
Looking for good resources on modifying unity SA 0 Answers
Must I attach every script to a gameobject in order to work ? 2 Answers
Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 Answers