Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by possmonaut · Dec 04, 2017 at 11:15 AM · rigidbody2daddforcecomponentexplosionfindobjectoftype

Help Creating Explosion Knockback

So I've tried a few different approaches to getting this working and none seemed to have worked, so I'm going to ask here.

What I'm trying to accomplish is have a barrel explode when it collides with the player, knocking back all Rigidbody2D with a certain radius of the object.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BarrelCollision : MonoBehaviour {
 
     private Collider[] colliders; 
     private Rigidbody2D rb2d;
     //private Rigidbody2D[] all_rigidbodies;
     public float radius = 10f;
     public float BlastForce;
 
 
 
     // Use this for initialization
     void Start () {
         rb2d = GetComponent<Rigidbody2D> (); 
 
         Vector2 SpawnHop = new Vector2 (0f, 6f); 
 
         rb2d.AddForce (SpawnHop, ForceMode2D.Impulse); 
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     public void Explode (){
         Rigidbody2D all_rigidbodies = (Rigidbody2D)FindObjectOfType(typeof(Rigidbody2D));
 
         foreach (Rigidbody2D r in all_rigidbodies)
         {
             if (Vector2.Distance(r.transform.position, transform.position) < 6)
                 {
                 float px = r.transform.position.x - transform.position.x;
                 float py = r.transform.position.y - transform.position.y;
                 r.AddForce ((Vector2 (px, py) * BlastForce), ForceMode2D.Impulse);
                 }
                 }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.CompareTag ("Player")) 
         {
 
             Explode ();
 
 
             /*colliders = Physics.OverlapSphere (transform.position, radius);
 
             foreach (Collider col in colliders) 
             {
                 if (col.GetComponent<Rigidbody2D> () != null)
                 {
                     Rigidbody2D rb2dother; 
                     rb2dother = col.GetComponent<Rigidbody2D> ();
 
                     Vector2 dir = (rb2d.transform.position - rb2dother.transform.position);
 
                     rb2dother.AddForce ((dir * BlastForce), ForceMode2D.Impulse);
                 }
 
             }*/
             }
         }
     }
 
 

The code that's not commented out threw me this error: Assets/BarrelCollision.cs(32,29): error CS1579: foreach statement cannot operate on variables of type UnityEngine.Rigidbody2D because it does not contain a definition for GetEnumerator or is inaccessible

The code that IS commented out didn't work at all. The Player object hit the barrel and just knocked it over (I have two colliders on it, one set to trigger and one not)

If any can help me out it would be greatly appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

118 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 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 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 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 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 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

plz I need a script of diagonal moving for rigidbody 2d 0 Answers

Predicting max jump height of a force impulse 1 Answer

2D Top-Down Character: How to make my character move using physics? 1 Answer

How to make arrows fly to user mouse position? 0 Answers

RigidBody2D.AddForce() horizontally is not working! 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