Unity falling body pendulum behaviour
- by user3447980
I wonder if someone could provide some guidance.
Im attempting to create a pendulum like behaviour in 2D space in Unity without using a hinge joint.
Essentially I want to affect a falling body to act as though it were restrained at the radius of a point, and to be subject to gravity and friction etc.
Ive tried many modifications of this code, and have come up with some cool 'strange-attractor' like behaviour but i cannot for the life of me create a realistic pendulum like action.
This is what I have so far:
startingposition = transform.position; //Get start position
newposition = startingposition + velocity; //add old velocity
newposition.y -= gravity * Time.deltaTime; //add gravity
newposition = pivot + Vector2.ClampMagnitude(newposition-pivot,radius); //clamp body at radius???
velocity = newposition-startingposition; //Get new velocity
transform.Translate (velocity * Time.deltaTime, Space.World); //apply to transform
So im working out the new position based on the old velocity + gravity, then constraining it to a distance from a point, which is the element in the code i cannot get correct. Is this a logical way to go about it?