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 /
avatar image
0
Question by Tona999 · Apr 21, 2020 at 10:08 PM · rigidbodyforcerigidbody.addforcerigidbody.velocitywind

How would you code a Wind script?

Hi! I have a simple player movement script where I manually set the velocity of the player's rigidbody. I am not using Rigidbody.AddForce() because I found it doesn't feel right for my type of game.

Now, in some of the levels I have what I call "wind zones" where an external force needs to be applied to the player based on the wind strength and the distance from the center of the wind as long as the player is inside the wind zone.

The question is, how would you program the wind zone? I would like to keep the wind zone as separate from the player as possible.

I could for example check if the player is inside of a wind zone in the player's script and and set the velocity vector accordingly, but then I have no code separation.

I also though about a Composition model, where an external force script is added to the player when the wind zone is entered. That way the force script will manipulate the player's velocity in the Update method. But which Update is called first, the player's Update or the force's Update? Is the order guaranteed? The fact there needs to be a specific order for it to works makes me think it's not a good idea. I would also like to be able to add the force script to any other object in the scene, so this approach is not good either.

I also though about a MovementModule mode, where I have a generic Movement Script and I plug in different movement modules, like wind zones, water etc. These modules should determine the velocity to assign to the player on each frame. But what if I have a wind zone in the water? Then only one module can work...

I am clearly confused, so if you also have any material where I can learn more about these things I would be very thankful!

Thank you.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by CiberX15 · Apr 22, 2020 at 03:34 AM

What I would do in this scenario is make a game object with a trigger. Then use OnTriggerStay() to find all objects currently inside the trigger. Then add velocity to any that have Rigidbodies. This also has the advantage of being completely independent of the player logic, and would work on any physics object that came into the wind area for a more consistent behavior (though you could check specifically for the player if you didn't want other objects to be effected). Something like this:

 private void  OnTriggerStay(Collider other)
 {
         Rigidbody hitR = other.gameObject.GetComponent<Rigidbody>();
         if(hitR != null)
         {
             hitR.velocity += windVelocity;
         }
 }


Now that would get overwritten if you are setting the players velocity every frame so you might still have to make some modifications to the player. In that case I would add a vector3 windvelocity variable to the player and instead of having the wind script add to velocity have it set that windvelocity varable. Then your player script could add windvelocity to the set velocity. Then zero the windvelocity. That should prevent order of operations issues since the windvelicty is only zeroed after it has been used, and it is only populated when the player is in the wind trigger area. in which case the wind code might look more like this:

 private void  OnTriggerStay(Collider other)
 {
         Player hitPlayer = other.gameObject.GetComponent<Player>();
         if (hitPlayer != null)
         {
                 hitPlayer.windVelocity = windVelocity
         }
         else
         {
                 Rigidbody hitR = other.gameObject.GetComponent<Rigidbody>();
                 if (hitR != null)
                 {
                         hitR.velocity += windVelocity;
                 }
         }
 }

And then in player script:

 void Update()
 {
         //Whatever your movement code does...
 
         velocity += windVelocity;
         windVelocity = vector3.zero;
 }


Finally while I don't think you should need to guarantee update order in this case since there are other ways to avoid the order of operation issues, it is possible if you need to for some other reason: https://docs.unity3d.com/Manual/class-MonoManager.html

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

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

How to limit only the Rigidbody.velocity from player input? 1 Answer

Moving rigidbody (Player) with addForce or Velocity ? 1 Answer

Rigidbody velocity slow on mobile device 0 Answers

rigidbody problem 1 Answer

add force to object that has 2 different rigid bodies 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