- Home /
Make a bullet to do damage
Hi, I have done a cannon bullet and I have seen this script here on unity
// A grenade // - instantiates a explosion prefab when hitting a surface // - then destroys itself
var explosionPrefab : Transform;
function OnCollisionEnter(collision : Collision) { // Rotate the object so that the y-axis faces along the normal of the surface var contact : ContactPoint = collision.contacts[0]; var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal); var pos : Vector3 = contact.point;
// Kill ourselves
Destroy(gameObject);
clone = Instantiate(explosionPrefab, pos, rot);
// Destroy the projectile
Destroy (gameObject);
}
Now I have a couple of problems, first of all, everytime my bullet explodes I found this message on the console:
The referenced script on this Behaviour is missing! UnityEngine.Object:Instantiate(Object, Vector3, QUaternion)
And the second problem I have it's that I want this bullet to hurt objects tagged as "Player" by using "function ApplyDamage (damage : int)" that Players have.
Know that this it's very silly and fool, but I'm a little bit noobish.
Lot of thanks for all
There is no such thing as silly and foolish questions, only silly and foolish answers. Remember we have all been there.
Answer by Dave 11 · May 08, 2011 at 09:36 PM
The referenced script on this Behaviour is missing! UnityEngine.Object:Instantiate(Object, Vector3, QUaternion)
For this, you'll need to add your prefab to the script by dragging it to the script component on the gameObject.
For the damage part: Add a variable named hitpoints to your player gameObject, then make it decrease when the bullet collides with the player.
$$anonymous$$y player have hitpoints, but I don't know how to make the bullet collision with player active the "function ApplyDamage (damage : int)" on the player
Your answer