- Home /
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#
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.
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
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613200158im_/https://answers.unity.com/themes/thub/images/avi.jpg)