- Home /
Health and Damage [C#]
Hello!
I have to scripts:
One bullet script where I have the damage and a damage script where the health of the enemy is set. When the bullet hits the enemy there should be damage. But I have a mistake in my scripts, but where is it?
Bullet Script:
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
public Rigidbody bullet;
public float Force = 2000.0f;
public AudioSource Shoot;
public Transform spawnpoint;
public float damage = 10.0f;
void OnCollisionEnter(Collision col)
{
col.gameObject.BroadcastMessage("ApplyDamage", 5.0F);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//if(canControl == true){
if(Input.GetButtonUp("Fire1")){
Rigidbody shot;
shot = Instantiate(bullet, spawnpoint.position, transform.rotation)as Rigidbody;
shot.rigidbody.AddForce(transform.forward * Force);
audio.Play();
}
//}
}
}
Damage Script:
using UnityEngine;
using System.Collections;
public class Damage : MonoBehaviour {
public float Health = 100.0f;
public Rigidbody deadReplacement;
// public Rigidbody dead;
public void ApplyDamage (float damage)
{
Health =- damage;
if (Health <= 0.0)
return;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Detonatin(){
Destroy(gameObject);
// If we have a dead barrel then replace ourselves with it!
if (deadReplacement) {
Rigidbody dead = (Rigidbody)Instantiate(deadReplacement, transform.position, transform.rotation);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
}
}
Answer by Chronos-L · Jan 15, 2013 at 02:07 AM
Your bullet script is not attached to the bullet your are firing. In
shot = Instantiate(bullet, spawnpoint.position, transform.rotation)as Rigidbody;
You make a bullet and apply force to it. But the OnCollisionEnter()
is not working as the shot
you made doesn't have the bullet script
You should move your
if( canControl == true ) {
...
}
to another Firing script. What you will have then is
a) Damage Script (Attached to an enemy)
b) Bullet Script (Attached to the Rigidbody bullet)
public class Bullet : MonoBehaviour {
public float damage = 10.0f;
void OnCollisionEnter(Collision col)
{
col.gameObject.BroadcastMessage("ApplyDamage", damage);
}
}
c) Firing Script (Attached to an empty object or your player)
public class Firing : MonoBehavior {
public Bullet bullet;
public float Force = 2000.0f;
public AudioSource Shoot;
public Transform spawnpoint;
void Update () {
//Nothing's changed from your previous code
if(canControl == true){
...
}
}
}
Friendly Tips: Check the setup of your bullet and enemies ( Collider & Rigidbody ), then use a Debug.Log("Collision with " + col.gameObject.name)
to check whether a collision has occured.
Lastly, this is not related to the question but
public void ApplyDamage (float damage)
{
Health =- damage;
if (Health <= 0.0) // <---What does this one do? Are you planning to call Detonatin() here?
return;
}
Ok thanks for the help. And yes I am planning to call a Detonation where a Rigidbody should be called when the enemy is dead.
Answer by potu1304 · Jan 19, 2013 at 01:10 PM
I tried to add a rigidbody when the enemy is dead, so that it is not moving anymore. But it does not really works.
using UnityEngine;
using System.Collections;
public class Damage : MonoBehaviour {
public float Health = 100.0f;
public Rigidbody deadReplacement;
// public Rigidbody dead;
public bool deadTrue = false;
public GameObject gameObject;
public void ApplyDamage (float damage)
{
Health =- damage;
if (Health <= 0.0){
deadTrue = true;
}
}
// void OnTriggerEnter(Collider bullet)
// {
// GetComponent<Bullet>();
// Health =- Bullet.damage;
//
//
// }
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Detonatin(){
Destroy(gameObject);
// If we have a dead barrel then replace ourselves with it!
if (deadTrue = true){
Destroy(gameObject);
Rigidbody dead = (Rigidbody)Instantiate(deadReplacement, transform.position, transform.rotation);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
}
}
Where do you called your
Detonatin()
?I am not able to see the reason behind the variable
bool deadTrue
Is
dead.rigidbody.velocity
a correct syntax? I question this because you declareddead
as a typeRigidbody
.
Your answer
Follow this Question
Related Questions
bullet damage problem 1 Answer
Take health from enemy 3 Answers
damage system 0 Answers
Health and damage reciever 1 Answer
Instantiate on value change 2 Answers