Making Inverse Pendulum ?
I am building a metronome in unity but facing a problem, the pendulum of the metronome starts fine but rather than maintaining its max and minimum angle after 3-4 swings its starts rotating anti clockwise.
Code
using UnityEngine;
using System.Collections;
public class Pendulum : MonoBehaviour {
#region Public Variable
public Rigidbody2D body2d;
public float leftPushRange;
public float rightPushRange;
public float velocityThreshold;
// Use this for initialization
void Start () {
body2d = GetComponent<Rigidbody2D> ();
body2d.angularVelocity = velocityThreshold;
}
// Update is called once per frame
void Update ()
{
Push ();
}
public void Push()
{
if (transform.rotation.z > 0
&& transform.rotation.z < rightPushRange
&& (body2d.angularVelocity > 0)
&& body2d.angularVelocity < velocityThreshold)
{
body2d.angularVelocity = velocityThreshold;
}
else if (transform.rotation.z < 0
&& transform.rotation.z > leftPushRange
&& (body2d.angularVelocity < 0)
&& body2d.angularVelocity > velocityThreshold * -1)
{
body2d.angularVelocity = velocityThreshold * -1;
}
}
}
#endregion
Game Object settings
untitled.png
(59.6 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612091450im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Forcibly halt movement of slow rigidbodies, but not falling ones. (2D) 1 Answer
Making 2d grid movement 0 Answers
How To Make My Player Only Move on the ground? 1 Answer
When friction on physic material 2d is at 0 my player slide 0 Answers
When using transform.Translate and Rotate It moves weared. 0 Answers