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 BlacKatFever · Oct 10, 2011 at 03:02 PM · c#addexplosionforce

Alternatives to AddExplosiveForce

Hey all,

I'm currently working on something and have hit a bit of a snag. I'm trying to set it up so that when the user touches a point on his screen, a flower petal (onscreen gameobject) moves in the opposite direction.

The way I imagined it - I wanted to run a check for any "flower" gameobjects within a "X" - pixel radius and, if positive, apply force to the petal in the opposite direction of the touch (with power inversely proportional to the distance.)

Mechanically, I figured

a) touch instantiates an empty / invisible gameobject that performs a check within a given radius for any "flower gameobjects."

b) if positive, it emits force in a direction towards the petal inversely proportional to it's distance (meaning that the emitted force grows weaker depending on how far away it is from the petal.

c) after emitting its force, the object destroys itself.

After looking through the script reference, I decided to use Overlap.Sphere for detection and AddExplosionForce to add force to the petal.

Using the code below, I attached a script to a placeholder object (a sphere) and made it that a mouse click caused this object to add force to the petal if it was in range. Force was inverse to distance. The problem I'm having is that explosion force adds force from the bottom of the game object - pushing it up. It looks awkward when you tap above the petal and it shoots upward.

Are there any other alternatives to AddExplosionForce? I like how the strength tapers off with distance and it seems like a decent fix. (Mostly because I haven't found how to use AddForce in a conditional statement.) Or, if I may be so bold, do you see a better way to do this?

Code is as follows:

 using UnityEngine;

using System.Collections;

 public class Exploder : MonoBehaviour 

{

 public float radius = 5.0f;
 public float power = 10.0f;
 
 GameObject flower;
 
 void Start()
 {
     flower = GameObject.Find("Flower");    
 }
 
 void Update()
 {
     if(Input.GetMouseButtonDown(0))
     {
         Debug.Log("Mouse button down!");
         Detector();
     }
 }
 
 void Detector()
 {
     Debug.Log("Detector on.");
     Collider[] objectsInRange = Physics.OverlapSphere(transform.position, radius);
     foreach (Collider hit in objectsInRange)
         {
             if (!hit)
             Debug.Log("Miss!");
             
             if (hit.rigidbody)
             hit.rigidbody.AddExplosionForce(power, transform.position, radius, 3.0F);
             Debug.Log("Hit " + hit.rigidbody);
         }
 }

}

As tempting as it is, I'm not asking you to write this out for me in code. If you could suggest alternatives to AddExplosionForce or a different approach in general, I'll try to muddle through it on my own (since I'm still learning C# and Unity.)

Thanks for taking the time to respond!

BKF

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 FLASHDENMARK · Oct 10, 2011 at 04:31 PM

I don't quiet get it, but your explosion is applying to much force upwards and you want to change that?

hit.rigidbody.AddExplosionForce(power, transform.position, radius, 3.0F);

That could be an easy fix. In the Docs it mentions a upwardsModifier I guess if you change your last parameter(3.0F) with something else to get an effect you like maybe to this:

hit.rigidbody.AddExplosionForce(power, transform.position, radius, 1.0F);

I have never tried messing with that parameter, but I am certain that, that is what you are looking for.

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 BlacKatFever · Oct 11, 2011 at 05:49 AM 0
Share

Looks like this did it.

On one hand, I'm glad it was so easy to fix. One the other, disappointed that it was written on the page and I failed to notice it. :(

Thanks for the help!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem with AddExplosionForce / Exploding 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 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