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
0
Question by PerfectOrigin · Jan 11, 2012 at 03:54 AM · damage

DamageScript

Why does this script only work with a rigidbody? And how can I make it work without a rigidbody? I don't want to use a rigidbody because my AI reacts poorly when a rigidbody is attached.

 var hitPoints = 100.0;
 function ApplyDamage (damage : float) 
 {
     if (hitPoints <= 0.0)
         return;
         
     hitPoints -= damage;
     if (hitPoints <= 0.0) 
     {
         BroadcastMessage ("Detonate");
     }
 }
 
 function Detonate () 
 {
     Destroy(gameObject);
 }

Rocket Launcher Script

 var projectile : Rigidbody;
 var initialSpeed = 20.0;
 var reloadTime = 0.5;
 var ammoCount = 20;
 private var lastShot = -10.0;
 var damage = 5.0;
 
 function Fire () 
 {
     var direction = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     
     // 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--;
     }
 }

Rocket Script

 // The reference to the explosion prefab
 var explosion : GameObject;
 var timeOut = 3.0;
 
 // Kill the rocket after a while automatically
 function Start () {
     Invoke("Kill", timeOut);
 }
 
 
 function OnCollisionEnter (collision : Collision) {
     // Instantiate explosion at the impact point and rotate the explosion
     // so that the y-axis faces along the surface normal
     var contact : ContactPoint = collision.contacts[0];
     var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
     Instantiate (explosion, contact.point, rotation);
 
     // And kill our selves
     Kill ();    
 }
 
 function Kill () {
     // Stop emitting particles in any children
     var emitter : ParticleEmitter= GetComponentInChildren(ParticleEmitter);
     if (emitter)
         emitter.emit = false;
 
     // Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
     transform.DetachChildren();
         
     // Destroy the projectile
     Destroy(gameObject);
 }
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 PerfectOrigin · Jan 11, 2012 at 04:32 AM 0
Share

Is it my Rocket Launcher Script from the Fps tut then? Or my Rocket script?

avatar image PerfectOrigin · Jan 11, 2012 at 06:11 AM 0
Share

I have a cube with ai and it is supposed to follow the player. When a rigid body is attached to the cube it can be killed but just hovers in the air, when no rigid body is attached the cube chases the player but cannot be killed. I want my cube to chase the player and be able to be killed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by adrenak · Jan 13, 2012 at 07:26 PM

In the FPS tutorial the explosion prefab that is instantiated when the rocked hits any collider, its script searches for rigidbodys near it so the forces can be applied to them, if there is any rigidbody near it, then it applies force and then it calls for ApplyDamage function in them. The reason why your enemy cube is not destroyed when you fire the rocket is because its not a rigidbody.

To solve the problem do this :

  1. tag all the enemies as "enemy" using the tag manager

  2. in the script attached to the explosion prefab, there are these statements

      for (var hit in colliders) {
             if (!hit)
                 continue;
             
             if (hit.rigidbody) {   
                 hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
                             
                 var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);
                 var distance = Vector3.Distance(closestPoint, explosionPosition);
     
                 // The hit points we apply fall decrease with distance from the hit point
                 var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
                 hitPoints *= explosionDamage;
     
                 // Tell the rigidbody or any other script attached to the hit object 
                 // how much damage is to be applied!
                 hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
             }
         
     
    
    

Here you can see that there are 2 if statements , they are : if(!hit) AND if(hit.rigidbody). Add these statements inside the for loop along with them.

 if(hit.gameObject.tag == "enemy"){
     hit.gameObject.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
 
 }

This makes sure that ApplyDamage function is called in enemies that are without rigidbody components.

Hope this helps! Tell me if you have any problems.

Comment
Add comment · Show 1 · 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 adrenak · Jan 15, 2012 at 06:01 AM 0
Share

Tell me if it works out!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Damage taking? 1 Answer

Attach object as child using code 1 Answer

Help with Basic Movement Script 1 Answer

NEED HELP CODING, CANT FIND MISTAKE 2 Answers


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