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 SirBoboHobo · Apr 27, 2015 at 05:28 PM · randomaddforce

Applying random forces to all instantiated gameobjects

OnDeath of my character I'm disabling him and instead instantiate 6 prefabs of smaller balls to give it a look of an explosion, but I want to fly those 6 prefabs all over the place, like to add random force to each other them, so each flies randomly to some side.

Comment
Add comment · Show 4
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 maccabbe · Apr 27, 2015 at 05:31 PM 0
Share

What have you tried so far?

avatar image SirBoboHobo · Apr 27, 2015 at 07:28 PM 0
Share

foreach (GameObject i in obj) { Instantiate(i, transform.position,Quaternion.identity); i.rigidbody2D.AddForce(new Vector2(Random.Range(-10f,10f), Random.Range(-10f,10f)), Force$$anonymous$$ode2D.Impulse); } I tried something like that obj is the GameObject[] with 6 prefabs in it

avatar image Bonfire-Boy · Apr 28, 2015 at 10:13 AM 1
Share

Each time through your loop, Instantiate creates a new object, a clone of the i prefab. But then you apply your force to i ins$$anonymous$$d of to the new object. Try something like

 foreach (GameObject i in obj) 
 { 
 GameObject g = Instantiate(i, transform.position,Quaternion.identity) as GameObject; 
 g.rigidbody2D.AddForce(new Vector2(Random.Range(-10f,10f), Random.Range(-10f,10f)), Force$$anonymous$$ode2D.Impulse); 
 }
avatar image SirBoboHobo · Apr 28, 2015 at 03:32 PM 0
Share

Bonfire Boy did the trick :) Thank you very much!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Apr 27, 2015 at 07:32 PM

Try this:

http://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html

then you can change the material and/or rigidbody values so that they seem to get random forces. Some will go further than others.

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 SirBoboHobo · Apr 28, 2015 at 03:30 PM 0
Share

THat's exactly what I was looking for. the problem is I'm developing a 2D game, and theres not Rigidbody2D.AddExplosionForce, anything similar to this for 2D?

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

20 People are following this question.

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

Related Questions

Kicking object with random direction and force 2D 0 Answers

Random instantiate at same frame with each instantiate having unique random direction 1 Answer

Semi Random Monster Spawner 3 Answers

using Switch and Case for 50% chance of occurance? C# 3 Answers

Teleporting camera and gameobjekt togather at random? 0 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