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 DOGY149 · Jul 11, 2012 at 01:25 PM · wind

Is this possible?

Well,Is there some sort of an easy way in Unity how to make a zone where from time to time will a strong wind appear and will turn objects with rigidbody down?Like when you walk into a strong water flow.

Comment
Add comment · Show 3
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 bubblegumsoldier · Jul 11, 2012 at 01:51 PM 0
Share

this is a very complex script but maybe look at the windzone effect... (I don't understand it but it's mybe a tip for u)

avatar image DOGY149 · Jul 12, 2012 at 09:22 AM 0
Share

Thanks for pointing me into the right direction dude.I think i understand it.But there is one problem. $$anonymous$$y char doesnt have a character controler.Its a simple sphere with some movement,jump scripts.Can i change the var to get component rigidbody?Because how will it recognize my ball?Thx again

avatar image AlucardJay · Jul 13, 2012 at 06:29 AM 0
Share

I have added an answer with some script that may help =]

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by AlucardJay · Jul 11, 2012 at 07:54 PM

Yes it is possible. Maybe do a SphereCast in the direction of the flowing water, for any colliders in that cast that have a rigidbody, add force. I like this idea better than an invisible collider and OnTriggerEnter, but those are 2 methods.

http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCast.html

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

EDIT : I have made a script from the following Unity Script References :

http://docs.unity3d.com/Documentation/ScriptReference/Physics.RaycastAll.html

http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCastAll.html

castDirection is a world-space Vector3; meaning if you put (-1,0,0), then the direction is pointing negatively along the X-axis; put (0,1,0) and the direction running positively along the Y-axis(pointing up). Try Different values for x,y and z, just keep them between -1 and 1.

I have now included a linedraw-er in the script, you can see what is affected by changing these values. You shouldn't include this in your build, it's just handy for showing where the spherecast is in the editor while designing and building.

castRadius, castDistance and amountOfForce should be self-explanatory =]

EDIT 2 : this script has been edited to include a time for the flow to be active and then time for the flow to be inactive. Set the flowTime (currently 10seconds on, 10seconds off) :

 #pragma strict

 private var castDirection : Vector3;
 private var castRadius : float;
 private var castDistance : float;
 private var amountOfForce : int;
 private var flowCounter : float = 0.0;
 private var isFlowing : boolean = true;
 private var flowTime : float = 10.0;

 function Start()
 {
     // build direction vector * Note : this is in World-Space
     castDirection = Vector3( -1, 0, 0 );

     // Radius of SphereCast
     castRadius = 3.0;

     // Distance of SphereCast
     castDistance = 9.5;

     // How much force to push on the rigidbody's
     amountOfForce = 100;
 }

 function Update() 
 {
     flowCounter += Time.deltaTime;
     
     if (flowCounter < flowTime)
     {
         isFlowing = true;
     }
     
     if (flowCounter > flowTime)
     {
         isFlowing = false;
     }
     
     if (flowCounter > (flowTime * 2.0))
     {
         isFlowing = true;
         flowCounter = 0.0;
     }
 }

 function FixedUpdate() 
 {
     if (isFlowing)
     {
         // Show SphereCast in editor (this is optional)
         ShowSphereCast();

         // create an array to store all rayhits
         var hits : RaycastHit[];

         // Physics.SphereCastAll( position : Vector3, radius : float, direction : Vector3, distance : float );
         hits = Physics.SphereCastAll( transform.position, castRadius, castDirection, castDistance );

         for (var i = 0;i < hits.Length; i++) {

             var hit : RaycastHit = hits[i];

             Debug.Log("hit " + hit.collider.transform.gameObject.name);

             if ( hit.collider.transform.gameObject.rigidbody != null )
             {
                 hit.collider.transform.gameObject.rigidbody.AddForce( castDirection * amountOfForce * Time.deltaTime );
             }
         }
     }
 }

 function ShowSphereCast() // (this is optional)
 {
     Debug.DrawRay( transform.position, castDirection * castDistance, Color.cyan );
     var origin : Vector3 = transform.position;
     var segments : int = 50;
     var calcAngle : float = 0;
     var posX : float[] = new float[segments + 1];
     var posY : float[] = new float[segments + 1];
     // Calculate Arc
     for (var i:int = 0; i < segments + 1; i ++)
     {
         posX[i] = ( Mathf.Sin( calcAngle * Mathf.Deg2Rad ) * castRadius ); //  + ( i * curvage );
         posY[i] = Mathf.Cos( calcAngle * Mathf.Deg2Rad ) * castRadius;
         calcAngle += 360.0 / parseFloat(segments);
         // Show outside of SphereCast
         Debug.DrawRay( Vector3( 0, posY[i], 0 - posX[i] ) + origin, castDirection * castDistance, Color.red );
         // Show origin of SphereCast
         Debug.DrawLine( Vector3( 0, posY[i], 0 - posX[i] ) + origin, origin, Color.red );
     }
 }

I have a web build of an example to show it works. The above script is on an empty gameObject, just under the white pipe =]

wasd or arrows to move green sphere : rigidbodystream webbuild

Comment
Add comment · Show 10 · 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 DOGY149 · Jul 13, 2012 at 06:39 AM 0
Share

Wow thanks dude, this is exactly what i ment!I never could have done this alone.Thumbs up to this guy =]

avatar image DOGY149 · Jul 13, 2012 at 10:56 AM 0
Share

By the way.Which number represents the direction?This is a very usefull script which can be used in more things :).

avatar image AlucardJay · Jul 13, 2012 at 11:30 AM 0
Share

When you SphereCast, the direction is the 3rd number Vector3

 // Physics.SphereCastAll( position : Vector3, radius : float, direction : Vector3, distance : float );

AddForce is also given as a Vector3

 // ....rigidbody.AddForce( Vector3( -100 * Time.deltaTime, 0, 0) );
avatar image DOGY149 · Jul 13, 2012 at 04:15 PM 0
Share

So change the number of the Vector right?In my movement script ive got left and right on Vector3. So i suppose Vector2 is for up and down?

avatar image AlucardJay · Jul 13, 2012 at 07:03 PM 0
Share

I have re-written my original answer. It is much more detailed, and exposes some variables for you to see what is happening. Also, I am quite proud of the Debug LineCaster to show where the SphereCast is affecting the world, only seen in the editor view. Check out my edited answer =]

Show more comments
avatar image
-1

Answer by kalekip1 · Jul 11, 2012 at 08:48 PM

Everything is possible, you just need to know how :)

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

7 People are following this question.

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

Related Questions

Cloud recognition in Vuforia 0 Answers

is it possible to create script for wind zone? 1 Answer

Animated clothes 1 Answer

Accessing a Variable in another script for (SLIDER) 1 Answer

How can I give an object realistic wind? 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