- Home /
How can i make a ray cast take health from enemies
How can i make a ray cast take health from enemies So I have a raycast script and a health script. I've been chopping and changing it to try and get it to work but so far have been unsuccessful. I run into two main problems.
First of all there is a problem where it will only take health from one instance of a zombie and not the others (So i manage to "kill" one zombie and the rest are invincible)
Secondly I'm getting this error "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."
I'm getting this even when I do check to see if the object is null.
Below is the code I currently have
RayCast Script:
using UnityEngine; using System.Collections;
public class ShotGunAttack : MonoBehaviour {
public GameObject target;
public float power = 200;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
//Attack();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo, 25.0f))
{
hitInfo.rigidbody.AddForceAtPosition(ray.direction * power, hitInfo.point);
if(hitInfo.rigidbody.tag == "Zombie" && hitInfo.rigidbody != null); {
ZombieHealth zh = (ZombieHealth)target.GetComponent ("ZombieHealth");
zh.AdjustHealth (-10);
}
}
}
}
} Health script:
using UnityEngine; using System.Collections;
public class ZombieHealth : MonoBehaviour {
public int fullHealth = 100;
public int health = 100;
public float healthBar;
// Use this for initialization
void Start () {
healthBar = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AdjustHealth (0);
}
public void AdjustHealth(int adj) {
health += adj;
if (health <= 0) {
health = 0;
if (gameObject != null) {
Destroy (gameObject);
}
}
if (health > fullHealth) {
health = fullHealth;
}
} } I've been stuck on this for a couple of weeks so any and all help would be much appreciated.
Answer by robertbu · Mar 01, 2014 at 11:52 PM
You need to get your ZombieHealth from the object you hit, not from the 'target' (which is only initialized once). Change line 31 to:
ZombieHealth zh = hit.collider.GetComponent<ZombieHealth>();
Note this line depends on any game object that is tagged 'Zombie' having a 'ZombieHealth' script.
Answer by black1ops22 · Mar 17, 2015 at 07:15 AM
Try this out:
using UnityEngine; using System.Collections; //Bulletspawnpoint MUST be named Bulletspawnpoint public class Tryintoscriptpewpews : MonoBehaviour {
public Rigidbody Bullet; // Bullet, Bulletspawnpoint, Automatic, isShooting,Bulletsound, Magsize, Ammo, firerate
public GameObject Barrelflash;
public GameObject Bulletholesmoke;
public GameObject Bulletspawnpoint;
public GameObject Bullethole;
public GameObject Blood;
public bool Automatic;
public float Range = 100f;
public float Firerate = 8f;
public float Magsize = 30f;
public float Amountofclips = 3f;
public float Damage = 15f;
private float Counter = 0f;
private bool isShooting;
public AudioClip Bulletsound;
RaycastHit hit;
// These are the customizable values. // Create a spawnpoint and place it infront of ze gun
void Update(){
Counter += Time.deltaTime;
if(Automatic == true) {
if(Input.GetKey (KeyCode.Mouse0) && Firerate > Counter){
isShooting = true;
if(isShooting == true){Fire();}
}
Counter = 0f;
}
else {
if(Input.GetKeyDown (KeyCode.Mouse0)){
isShooting = true;
if(isShooting == true){Fire();}
}
}
}
void Fire (){
Instantiate (Bullet, GameObject.Find ("Bulletspawnpoint").transform.position, GameObject.Find ("Bulletspawnpoint").transform.rotation);
Bullet.velocity = Vector3.forward * 8;
AudioSource.PlayClipAtPoint (Bulletsound, transform.position);
if (Physics.Raycast (GameObject.Find ("Bulletspawnpoint").transform.position, transform.forward, Range)) {
if(hit.collider.gameObject.tag == "Player"){
Instantiate(Blood, hit.point, hit.transform.rotation);
hit.collider.gameObject.SendMessage("ApplyDamage", Damage);
}
}
}
}
i think you can code a health script for the zombies. BTW not all features of this script are done yet...
Your answer
Follow this Question
Related Questions
Using RayCast to do Continuous Shooting? 1 Answer
Unity c# Raycast axis stuck on one axis 0 Answers
Using raycast gun to take down life of enemy when shot 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers