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 K1kk0z90 · Jul 19, 2010 at 01:26 PM · fpsforeachclampexplosion

Explosion-Simple.js script of FPS Tutorial

Hi all, I'm new to Unity. I've read GUI Essentials and Scripting Essentials tutorials, and now I'm having some problems understanding a script of the FPS Tutorial Part 2.

var explosionRadius = 5.0; var explosionPower = 10.0; var explosionDamage = 100.0;

var explosionTime = 1.0;

function Start () {

var explosionPosition = transform.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);

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

}

// stop emitting ? if (particleEmitter) { particleEmitter.emit = true; yield WaitForSeconds(0.5); particleEmitter.emit = false; }

// destroy the explosion Destroy (gameObject, explosionTime);

}

In particular, I don't understand some instructions in the for-each: 1) Why there is "if (!hit) continue"? for-each should select every element in colliders, shouldn't it? 2) I don't understand these instructions: 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);

Thank you very much for your help! :)

Comment
Add comment
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

1 Reply

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

Answer by Julian-Glenn · Jul 19, 2010 at 02:23 PM

1) Here is what Continue is doing: If there are no "hits" then break the loop.

2) hitPoints is how much damage to apply to the object that was hit. the calculation (distance / explosionRadius) is Mathf.Clamp01 clamped to prevent a value < 0 or > 1 You then send a message to the object and its children to run the ApplyDamage() function using the value hitPoints that we calculated using the (distance / explosionRadius) formula.

Comment
Add comment · Show 4 · 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 skovacs1 · Jul 19, 2010 at 03:44 PM 0
Share

I think a clearer explanation of the continue statement has nothing to do with there being no "hits". If there were no hits, the Physics.OverlapSphere would return an empty array and the for-each would have nothing to iterate over. I think you mean to say that if the returned hit collider object is null, then continue on to the next one to avoid dealing with the null object.

avatar image K1kk0z90 · Jul 19, 2010 at 04:41 PM 0
Share

Thank you, now I understood better the answer to the first question! :) I still have a little doubt: distance / explosionRadius is always a value between 0 and 1. Why do we use $$anonymous$$athf.Clamp01()? Also, what's the difference between Component.Send$$anonymous$$essageUpwards() and GameObject.Send$$anonymous$$essageUpwards()? Thank you again!

avatar image Julian-Glenn · Jul 19, 2010 at 05:21 PM 0
Share

@skovacs - yes you're right :)

@$$anonymous$$1kk0z90 - mathF.clamp01 creates a fraction and prevents negative numbers so if you're sure your code never results in a negative number or a number higher than 1.0 I'm not sure why it's there either. I am pretty terribad at math :) GameObject.Send$$anonymous$$essageUpwards works when attached to a gameObject like a mesh, component works when attached to a component like a rigidbody or script. It's not very clear on the Unity doc's but that's all I can figure.

avatar image K1kk0z90 · Jul 20, 2010 at 10:06 AM 0
Share

Thank you very much, now everything is clear! :)

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

How to make the enemy choose only one cover? 1 Answer

How do you make a ragdoll blow apart? 1 Answer

Explosive like in COD 4? 1 Answer

gun muzzle flashes 2 Answers

Clamping the Camera, according to the player ? 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