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 snobis · May 24, 2015 at 12:28 PM · physicswind

How to add wind in 2D physics game

I am trying to use wind to affect my boxes that are flying around...i created a empty gameobject with a Box Collider 2D and then i add this script that i found online

 public class WindComponent : MonoBehaviour 
 {
         // To use:
         // 1. Create an empty game object
         // Add a Collider2D to the empty object. I used a standard Box Collider 2D, as a trigger, to create an isolated square that applies "wind"
         // 3. Add this WindComponent to the empty object, then adjust the Force (Vector2). You can adjust this in the editor or in the script, as it is a public property
         // Note: Only works on game objects that have the Rigid Body 2D and Collider 2D components
  
         // Directional force applied to objects that enter this object's Collider 2D boundaries
     public Vector2 Force = Vector2.zero;
  
         // Internal list that tracks objects that enter this object's "zone"
     private List<Collider2D> objects = new List<Collider2D>();
  
        // This function is called every fixed framerate frame
     void FixedUpdate()
     {
                 // For every object being tracked
         for(int i = 0; i < objects.Count; i++)
         {
                         // Get the rigid body for the object.
             Rigidbody2D body = objects[i].attachedRigidbody;
  
                         // Apply the force
             body.AddForce(Force);
         }
     }
  
     void OnTriggerEnter2D(Collider2D other)
     {
         objects.Add(other);
     }
  
     void OnTriggerExit2D(Collider2D other)
     {
         objects.Remove(other);
     }
 }

The Script gives me the following errors on:

-private List objects= new List(); -objects.count -Rigidbody2D body = objects[i].attachedRigidbody; ( attachedRigidBody) -objects.Add(other); -objects.Remove(other);

I dont know if i have to add something to the List objects for the things to work. Thank you very much. I am creating the script as c#

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 Fornoreason1000 · May 23, 2015 at 05:23 AM 0
Share

can you tell us what the errors actually are? does it only give errors on runtime?

e.g Argument Out of Range? ,NUll Reference?

Also please give credit where it is due, in Australia Copyright is automatic and that script is their intellectual property.

avatar image grji09020 · Sep 25, 2019 at 11:30 AM 0
Share

Tanks for your help :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by klh_js · Nov 07, 2016 at 07:54 PM

The question is probably not followed by the author anymore, but it pops up in Google so here is the answer:

The script attached works perfectly, you just have to add

 using System.Collections.Generic;

at the top to have the List<> type available.

Can't edit the question - here is the original (I guess?) code: http://wiki.unity3d.com/index.php?title=WindComponent

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

Answer by muhammadtahiriqbal · Jun 10, 2017 at 09:38 AM

Create an invisible box in front of the fan. Then check if some object is inside that box. Apply movement to that object.

Create Empty gameobject Select the new gameobject Add Component-> Mesh -> Mesh filter Select from inspector -> Mesh filter -> mesh and set it to "cube" ( or what ever shape you want ) Add Component -> Physics -> Box collider Select from inspector -> Box collider -> Is Trigger to true ( checked ) Now, you have set up an collision trigger, that can detect if some object comes inside of it. Next, apply some force to push that object back or move it with translate directly.

Create new script to just created gameobject. Add inside that script these:

 void OnTriggerEnter(Collider other)
 {
      Debug.log("Object entered trigger");
 }
 void OnTriggerStay(Collider other)
 {
      Debug.log("Object is in trigger");
      // Here you add negative forces to object that is within the fan area
      // Other is the object, that should be pushed away
      Vector3 position = transform.position;
      Vector3 targetPosition = other.position;
      Vector3 direction = targetPosition - position;
      direction.Normalize();
      int moveSpeed = 10;
      other.position += direction * moveSpeed * Time.deltaTime;
 
 }
 void OnTriggerExit(Collider other)
 {
      Debug.log("Object left the trigger");
 }

or

 void OnTriggerStay2D(Collider2D other)
 {
     Debug.Log("Object is in trigger");
     player.rigidbody2D.AddForce (-Vector2.right*20000*Time.deltaTime);
 
 }

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

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

6 People are following this question.

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

Related Questions

How to Buffet an object floating in space? 1 Answer

Physics on a Robe (Kind of like the "Journey" character) 0 Answers

How to make building affected by wind? 0 Answers

Swipe Wind 2 Answers

Modify AddExplosionForce and allow upwardsModifier to any direction instead 3 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