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
0
Question by MegawhiteS2 · Jan 18, 2017 at 10:56 PM · 2d gamephysics2d

Perpetual Pendulum

Hello, I am fairly new to Unity and 2D game development but do have a background in development in general.

Within the game I am creating, I have an elongated object acting as a pendulum and have it attached to a HingeJoint2D. Below is the desired effect I wish to achieve:

alt text

A is my initial starting position at an 80 degree angle. When the scene starts, I want to begin to drop it to swing over to B which will be the same angle as A but on the other side. When my pendulum reaches point B, I want it to go back to point A, then back to point B etc. indefinitely.

I've used both transform.RotateAround and Quaternion.Slerp, however, I am unable to get it to fully reach point B with the pendulum-like slow down effect and for it to swing back over to point A. Code RotateAround only works in one direction while Quaternion.Slerp acts weirdly though I think it might be due to my lack of understanding in using it in code.

Any help would be greatly appreciated!

desired-effect.png (84.9 kB)
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
0
Best Answer

Answer by MelvMay · Jan 19, 2017 at 08:35 AM

Is there a reason why you cannot let 2D physics do the work for you?

There are several ways to do this but the most simple method is to represent the end of the pendulum only. To do this, add a GameObject with a Rigidbody2D with its bodyType set to Dynamic. Then add a DistanceJoint2D with its maxDistanceOnly set to true.

Now, because autoConfigureDistance defaults to true, move the GameObject to its starting position, either A or B. You should note that the joint draws a line from its anchored position to its current position and sets the distance automatically. Assuming you created the GameObject at the hinge position, this'll be set-up correctly. If the hinge position isn't correct then you can simply drag it in the scene view or modify it in the inspector by looking at the DistanceJoint2D and modifying its connectedAnchor position.

This setup means that the Dynamic Rigidbody2D will try to move downwards under the influence of gravity however the DistanceJoint2D will maintain a maximum distance to the connectedAnchor position. This causes the Rigidbody2D to move through the arc between A and B.

Note that you can control linear damping here with the Rigidbody2D drag. By default its zero.

Alternately, ignoring physics for now and just using Transform manipulation, you can achieve it various ways. I've attached a project that does just that and also includes a speed control and an explicit Quaternion from & to although you could easily change this to be an Euler-Z if you so desire; the results can be seen here.

Note that the example uses Quaternions that perform a 180-deg arc, obviously you can do your 160-deg arc by changing the from/to Quaternions.

Hope this helps, please mark this as the correct answer if it helps, thanks.

Attached Project


testpendulum.zip (140.1 kB)
Comment
Add comment · Show 3 · 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 MegawhiteS2 · Jan 20, 2017 at 07:20 AM 0
Share

After playing around, this is what I was able to find out.

Both DistanceJoint2D along with HingeJoint2D do provide a pendulum effect, however it is not perpetual (infinite). Over a period of time, the object will come to a complete stop at the bottom of the arc due to gravity.

I have tried setting the Angular and Linear Drag both to 0 without any effect.

I am currently playing with the idea of creating two empty game objects and setting both of those at position A and position B, then creating a LookRotation workaround with Quaternion.Slerp to see if this will give me the effect I want.

Just for a better visual, imagine a game level where you have an axe hanging from a hinge point. That axe will be an obstacle for the passing player as it will be infinitely swinging within its position without ever co$$anonymous$$g to a stop.

avatar image MelvMay ♦♦ MegawhiteS2 · Jan 20, 2017 at 09:57 AM 0
Share

I've updated my answer to include an attached project and description that manipulates the Transform directly ignoring physics completely (although you can obviously attach a $$anonymous$$inematic body and colliders if you so wish). The simple script includes a speed control as well.

The result is here.

Hope this helps, please mark my original answer as the correct answer if it helps, thanks.

avatar image MegawhiteS2 MelvMay ♦♦ · Jan 20, 2017 at 04:23 PM 0
Share

Did not realize at first that you created an empty object and placed the rope/box within that and had the script on the EO itself. Once I figured that out, I got your solution to work with what i wanted to do. Answer has been marked and it is much appreciated! THAN$$anonymous$$ YOUUU!

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

93 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

Related Questions

How do I get a ball to bounce inside of a circle? 1 Answer

Random freezes during the game 0 Answers

How to let player hit a boxcollider just once?(2D) 1 Answer

Sprites pixel colliding with 2d collider 0 Answers

Arkanoid-like bounce troubles 1 Answer


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