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:
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!
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.
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.
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.
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
Follow this Question
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