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 /
  • Help Room /
avatar image
1
Question by SuperSpasm · Jan 16, 2017 at 12:40 AM · physicsrigidbody2dmath

Oscillate object with forces/velocity

I want to oscillate my GameObject in a wave like form for it to appear floating. Now this works fine when I manipulate position manually using Mathf.Sin, but since that doesn't really play nice with collisions I'd like to do it with oscillating the force applied to the object, or even just velocity.

However, every time I try this in some form with adding forces, the object moves up, then pauses, then keeps moving up, (seems like velocity pingponging to 0 rather than oscillating from -foo to +foo) I got to my force formula by taking the 2nd derivative of the position formula and multiplying by mass. I checked wolfram and it seems like my derivatives are correct.

This is driving me crazy. is my math wrong or my code?

Forces version (doesn't work, just keeps going up)

 void FixedUpdate()
 {
     t += Time.fixedDeltaTime;
     float magnitude = rb.mass * (4*Pow(pi,2)/90)* Sin((t/3)*2* pi);
     rb.AddForce(magnitude * Vector2.up);
 }
 

Velocity version (oscillates but slowly moves downwards)

    void FixedUpdate()
     {
         t += Time.fixedDeltaTime;
         float magnitude = 2*pi/30 *Cos((t / 3) * 2 * pi);
         rb.velocity += magnitude * Vector2.up;
     }


Position version- works perfectly

     void FixedUpdate()
     {
         t += Time.fixedDeltaTime;
         float magnitude = 0.1f * Sin((t / 3) * 2 * pi);
         Vector3 newPos = transform.position;
         newPos.y += magnitude;
         transform.position = newPos;
     }

Notes:

  • t is a time counter, rb is the RigidBody2D component, Sin/Cos/Pow are wrappers for Mathf functions

  • my object has a RigidBody2D attached with gravity scale set to 0, mass set to 1 (tried auto mass, didn't seem to affect behavior).

  • force formula derivation:

       Pos(t)= 0.1*sin((t/3) *2pi)
         V(t) = Pos'(t) = (2pi/30)cos((t/3)*2pi)
         F(t) = m * a(t)
         F(t) = m * V'(t)
         F(t) = -m *(4pi^2/90)sin((t/3)*2pi)
    
    

also I tried this in various ways in Update using Time.deltatime and with Lerp. no dice.

Any help would be really appreciated :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MelvMay · Jan 16, 2017 at 07:47 AM

If you have a position you want a Rigidbody2D to move to then simply use MovePosition (you should never set the Transform position with a Rigidbody2D attached as adding a Rigidbody2D is asking Unity to give it control of the Transform). This calculates the velocity required to move to the specified position during the next fixed-update. Note that this doesn't alter the body velocity that you see via scripts at all; it'll have the same velocity after it has moved as it did before it moved. Because the body is moving under its own velocity, it'll detect collisions (especially continuous ones) correctly. Technically this is most useful for Kinematic body types however it does work for Dynamic body types as well.

Comment
Add comment · Show 2 · 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 SuperSpasm · Feb 07, 2017 at 11:49 AM 0
Share

This was a good fix for using collisions, thanks! :) However, I still don't know why my derived functions didn't work. So I'm leaving this question open in hopes that someone can point out my error or whether there's a bug in there somewhere

avatar image MelvMay ♦♦ SuperSpasm · Feb 07, 2017 at 12:28 PM 0
Share

I suspect the problem with the 'force' one is that you have not set the Rigidbody2D.gravityScale to zero on the Rigidbody2D therefore gravity is added to the velocity each fixed update.

When you simply set the position, it won't matter what the velocity is (even if it's continually increasing because of gravity) because you're constantly repositioning it back.

Try dumping out the Rigidbody2D velocity in the 'position' one and you'll see the velocity increasing.

avatar image
0

Answer by TheyLeftMe4Dead · Jan 16, 2017 at 04:55 AM

Without reading any of your code, if I were scripting this, I'd let Unity handle most of the hard work. First, I would allow Gravity to pull my GameObject. My code would look like this:

 Update () {
      if (transform.position < minHeight) {
           //AddUpwardsForce
      }
 }

This would keep pushing the GameObject up when it fell below X coordinates. I would adjust the oscillating speed by adjusting the object weight/ the force I apply to it.

Alternatively, I would change the Object's position rather than use velocity or force. Using Velocity or force means that the object will move a bit before the next calculation can be preformed. So instead, I would first calculate the next position then add/subtract from it's transform.position.y. This will mean that the object wouldn't move unless you specifically told it to in your script, allowing you more control over it.

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

122 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

Related Questions

How do I keep rigidbodies from pushing each other? 2 Answers

Collision sticking? 1 Answer

Unity2D Help with rigidbody character movement. 0 Answers

Whats the difference between setting rotation and angularVelocity of a rigidbody?,What the difference between setting angularVelocity and rotation of a rigidbody? 1 Answer

Rigidbody2D and non-trigger child colliders! 2 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