4
\$\begingroup\$

For a 2D scenario, I want to have a fan blow at an object and make the object move with the "wind". How can I simulate the wind force blown by the fan?

\$\endgroup\$
  • 3
    \$\begingroup\$ It's unclear what angle this should be approached from. Are you asking for an architecture (code) answer, or physics or graphics of the implementation? \$\endgroup\$ – Anko Jul 8 '14 at 9:17
7
\$\begingroup\$

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

This can be done in unity quite easily.

  1. Create Empty gameobject
  2. Select the new gameobject
  3. Add Component-> Mesh -> Mesh filter
  4. Select from inspector -> Mesh filter -> mesh and set it to "cube" ( or what ever shape you want )
  5. Add Component -> Physics -> Box collider
  6. 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");
}

That should do it or atleast set you on right direction. This was written in late hours, it's not tested, but should work. Let me know if this was not what you wanted.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Wow this was fast, I just posted this question yesterday without any hopes up, this is great and very useful, thanks! \$\endgroup\$ – Esser420 Jul 8 '14 at 9:10
  • \$\begingroup\$ Np. Mark it as an answer if later, if no better answer show up :) \$\endgroup\$ – Katu Jul 8 '14 at 9:13
  • \$\begingroup\$ Well I was actually looking for 2D, therefore I changed every function to its 2D correspondent. The trigger does detect, but does nothing, the moment it enters the trigger it automatically leaves it, the OnTriggerStay2D method is never called.. \$\endgroup\$ – Esser420 Jul 8 '14 at 9:32
  • \$\begingroup\$ Are you using latest Unity? 4.5+? There was a bug that caused it not to trigger in later versions. \$\endgroup\$ – Katu Jul 8 '14 at 10:46
  • \$\begingroup\$ actually, my bad, code works great :D thanks a lot, doesnt even look that hard but Im kind of a noob at this unity thing.. there is one question though, what is the mesh filter used for? \$\endgroup\$ – Esser420 Jul 8 '14 at 19:49
0
\$\begingroup\$

This is my way for achieving this effect:

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

}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.