Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jacques 1 · Apr 23, 2011 at 06:16 PM · damageaddinglauncherrocket

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; }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacques 1 · Apr 23, 2011 at 06:18 PM 0
Share

idk what happened why its choppy like that

avatar image Justin Warner · Apr 23, 2011 at 06:52 PM 0
Share

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!

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacques 1 · Apr 25, 2011 at 03:32 AM 0
Share

your worked best but my enemy has a rocket to and i want the rocket to hurt me too what could i do

avatar image Flafla2 1 · Apr 25, 2011 at 03:37 AM 0
Share

You attatch this script to the player, and tag anything that could hurt the player with "Enemy."

avatar image jacques 1 · Apr 25, 2011 at 03:41 AM 0
Share

can u hav multiple tags to one object?

avatar image Flafla2 1 · Apr 25, 2011 at 04:23 PM 0
Share

At the moment, no. However, you can also use gameobject.name, and just name the gameObject something.

avatar image jacques 1 · Apr 25, 2011 at 07:45 PM 0
Share

nothing happens:(

Show more comments
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacques 1 · Apr 24, 2011 at 08:03 PM 0
Share

wait so what would the rockets final script look like, i know basicly nothing about scripting

avatar image jacques 1 · Apr 25, 2011 at 12:48 AM 0
Share

complete script please

avatar image
0

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 &lt; 0)
     curHealth = 0;

 if (curHealth &gt; maxHealth)
     curHealth = maxHealth;

 if(maxHealth &lt; 1)
     maxHealth = 1;

 if(curHealth &lt; 1) { //die
     Destroy(gameObject);
 }

}

function OnCollisionEnter(collision : Collider){ if(collision.gameObject.tag == "Rocket"){ curHealth -= rocketDamage; } }

and add a tag Rocket to your rocket prefab.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

My rockets don't do damage? 1 Answer

How to make RPG launcher shoot the rocket? 1 Answer

Rocket projectile damage not applied on collision (message not sent/received) 1 Answer

my rocket fires in the wrong direction 4 Answers

Ai Aint Taking Damage From Bullet (FPS Rocket) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges