- Home /
adding damage to my rocket
i was watching the fps techzone tutorial and noticed the rockets dont do damage does anyone know how to add it? please help! rocket script:
var projectile : Rigidbody; var initialSpeed = 20.0; var reloadTime = 0.5; var ammoCount = 20; private var lastShot = -10.0;
function Fire () { // Did the time exceed the reload time? if (Time.time > reloadTime + lastShot && ammoCount > 0) { // create a new projectile, use the same position and rotation as the Launcher. var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
if it helps here is the machine gun script with damage from same download:
var range = 100.0; var fireRate = 0.05; var force = 10.0; var damage = 5.0; var bulletsPerClip = 40; var clips = 20; var reloadTime = 0.5; private var hitParticles : ParticleEmitter; var muzzleFlash : Renderer;
private var bulletsLeft : int = 0; private var nextFireTime = 0.0; private var m_LastFrameShot = -1;
function Start () { hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
function LateUpdate() { if (muzzleFlash) { // We shot this frame, enable the muzzle flash if (m_LastFrameShot == Time.frameCount) { muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward); muzzleFlash.enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
// We didn't, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () { if (bulletsLeft == 0) return;
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && bulletsLeft != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit;
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, hit, range)) {
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0)
Reload();
}
function Reload () {
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0) {
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () { return bulletsLeft; }
Hey man, just had to code-ify your code. Next time when posing code, just highlight it, and push the 10101 button up top =). Thanks!
Answer by Flafla2 1 · Apr 25, 2011 at 01:59 AM
So what Meater6 was trying to say was to add a hit points system. I commented it and simplified it to make it less confusing.
var damage = 10; //this variable is the HP of the character. //0 = dead, 10 = full health. //you can change this in the inspector.
function OnCollisionEnter (thing : Collision) { if(thing.gameObject.tag == "Enemy"){ //"thing" is the collision entity, //"gameObject" is the gameObject that collided with your rocket, // and "tag" is the tag of the gameObject that collided. Google "Unity3d tag" if you don't know what tags are. //basically if the tag of the collided gameobject is "Enemy," then...
//destroy the enemy thing
Destroy(thing.gameObject);
//and take away 1 damage.
damage -= 1;
}
}
function Update { if (damage == 0) { //Make damage not equal 0 so RocketDeath() doesn't happen twice damage = -1; RocketDeath(); } }
function RocketDeath() { //When the HP goes to 0, then do this. }
Attach this to your rocket, by the way. Cheers!
your worked best but my enemy has a rocket to and i want the rocket to hurt me too what could i do
You attatch this script to the player, and tag anything that could hurt the player with "Enemy."
At the moment, no. However, you can also use gameobject.name, and just name the gameObject something.
Answer by Meater6 · Apr 24, 2011 at 03:19 AM
To do damage, you must have something that takes the damage. Make a enemy status script, if don't have one already, for if you do, put it in your question.
Ok, you need to check to see if the rocket hit a valid target. Add a rocket damage script to the rocket you are instantiating. Using
OnColliderEnter (collider : Collision)
Add a "Enemy" tag to the thing your shooting at and check to see if the rocket hit something with that tag. If it has, send a message using
yourEnemyObject.SendMessage(SubtractHealth, damage);
in the rocket damage script. This will run the SubtractHealth function with parameters "damage" in the enemy status script. For the complete script, comment on this saying so.
EDIT:
Actually, an easier way is to just have a hitPoints variable, and subtract form that.
"RocketStatus" (The one that goes on the rocket itself)
var damage : float = 1;
function OnCollisionEnter (thing : Collision) { if(thing.gameObject.tag == "Enemy"){ thing.gameObject.SendMessage("TakeDamage",damage,SendMessageOptions.DontRequireReceiver); Destroy(gameObject); } }
"EnemyStatus" (The one that controls the hit points)
var hitPoints : float = 10;
function Update () { if(hitPoints <= 0){ DestroyObject(root); } }
These scripts are untested, but I am positive they will work fine. Hope this gives you a better idea of how this works.
wait so what would the rockets final script look like, i know basicly nothing about scripting
Answer by Aydan · Apr 25, 2011 at 02:05 AM
heres what i would do, this is a health script add this to the object you want to take damage:
var curHealth : float = 100; var maxHealth : float = 100; var rocketDamage : float = 10;
function Update () { AddjustCurrentHealth(0);
}
function AddjustCurrentHealth(adj) { curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if (curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
if(curHealth < 1) { //die
Destroy(gameObject);
}
}
function OnCollisionEnter(collision : Collider){ if(collision.gameObject.tag == "Rocket"){ curHealth -= rocketDamage; } }
and add a tag Rocket to your rocket prefab.