Swing effect on sudden stop

Hi ¨
I have crane and ROV attached to it.
I can move the crane horizontally by left and right arrow keys and vertically by up and down arrow keys.
but when I suddenly relase the keys, the ROV should get swing effect o it for few seconds due to sudden stop.
I tried with the script but it woks only for a second and rotation stops.
What loop can I used instead of this?
Please help.

var frequencyMin : float = 1; 
var frequencyMax : float = 2; 
var magnitude : float = 0; 
var timer : float =5.0; 

private var randomInterval : float; 

function Start () { 
randomInterval = Random.Range(frequencyMin, frequencyMax); 
} 


function Update() { 

if (Input.GetKeyDown (\"left\")) 
{ 
print(\"dwn\"); 

} 

/*transform.Rotate(Vector3.right * Time.deltaTime*5, Space.World); 
transform.Rotate(Vector3.left, Time.deltaTime*5, Space.World); 
// ... at the same time as spinning relative to the global 
// Y axis at the same speed. 
transform.Rotate(Vector3.up * Time.deltaTime*5, Space.World);*/ 


if ((Input.GetKeyUp (\"left\"))){ 
this.transform.position.y += (Mathf.Cos(Time.time * randomInterval) * magnitude); 
this.transform.eulerAngles.x+= (Mathf.Cos(Time.time * randomInterval) * 0.05); 
this.transform.eulerAngles.y+= (Mathf.Cos(Time.time * randomInterval) * 0.01); 
this.transform.eulerAngles.z+= (Mathf.Cos(Time.time * randomInterval) * 0.03); 
print(\"up\"); 


} 
}

[/code]

Hi, welcome to the forum!

How are you moving the crane object? It is probably easiest to use the physics engine for all of this. If you attach the ROV to the crane using a hinge joint or configurable joint, it should move and swing realistically as you move the crane.

I am using the script which has Translate and Rotate in it to move the crane.
I want swing the ROV on stop by physics engine.

I am using the script which has Translate and Rotate in it to move the crane.
I want swing the ROV on stop by physics engine.

Try adding a kinematic rigidbody to the crane arm and then add a joint to the ROV connecting it to the arm.

Another way to do this would be to instead use a non-kinematic rigidbody, and add forces, but using ForceMode.VelocityChange, which acts very similarly to translate and rotate.