# Balancing Forces + Gravity

I am having problem coding basic suspension i think i have Physics problem don’t know how to approach it.
I am trying to suspend a box in air using physics i think i am doing something wrong would like a new set of eyes

``````using UnityEngine;
using System.Collections;

public class Suspenzija : MonoBehaviour {

public float SuspensionRayLength;
public float SuspensionPower;
public Rigidbody kart;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void FixedUpdate ()
{
//Defining position,  bottom midle of Box
Vector3 midlebottom = transform.TransformPoint (new Vector3 (0f, -0.5f, 0f));

//SuspensionRay hitColector and executes ray for difined point
RaycastHit WhatRayHited;
if (Physics.Raycast (midlebottom, -transform.up, out WhatRayHited, SuspensionRayLength))
{
//Compresion Calculation
float RCompressionRatio = WhatRayHited.distance / SuspensionRayLength;
//Reversing it for using it as power multiplayer cuz multplaying it with 0.* is /
float CompressionRatio = 1 - RCompressionRatio;
//Clamp just to be sure xD
Mathf.Clamp (CompressionRatio ,0f ,1f);

//Adding Force to counter gravity so Box goes Up
kart.AddForceAtPosition (Vector3.up * CompressionRatio * SuspensionPower, midlebottom, ForceMode.Impulse);

//Waz geting jumping box like trambolin so i diceded to add counter force so if suspension almost
//extended i add force down, hoping for the best but not good solution, NEED HELP

//MY idea for stoping box jumping all time Not WOrking ATM
if  (CompressionRatio < 0.8f)
{
float  AntiCompressionRatio = 1f * CompressionRatio;
//Tryed adding first pure counter force but it dident work so i tryed to kinda limit it
//with math.clamp but still not geting stable suspension MY MAIN ISUE
Mathf.Clamp(AntiCompressionRatio,0.1f,0.6f);
{
kart.AddForceAtPosition (-Vector3.up * AntiCompressionRatio * SuspensionPower, midlebottom,ForceMode.Impulse);
}
}

//Green Ray for debuging
Debug.DrawRay (midlebottom, -Vector3.up, Color.green);
Debug.Log (WhatRayHited.distance + "Distanca of hit Amortizera");
Debug.Log (CompressionRatio + "Power of Amortizera");

}
else
{
//Red Ray for debugign
Debug.DrawRay (midlebottom, -Vector3.up * SuspensionPower, Color.red);
}
}
}
``````

Just increase the drag of your rigidbody.

Your logic just creates an oscillator. As the object falls down it increases in speed (due to constant acceleration caused by gravity). The closer you get to the ground the greater your counter force gets. At the point of zero force (when your counter force equals the gravitational pull) the object’s velocity is greatest so it continous moving downwards which will further increase your counter force. The net force is now pointing upwards and will slow down the object until it stops at a point. At this point your up ward force is largest which will accelerate it back upward. Since it’s a linear dependency it would bounce up and down forever.

When you add drag to the movement a bit of energy is “lost” each frame. Depending on how large the drag is the faster it will come to a halt at the zero force point.