How to smooth out crouch movement (smooth damp function not working)

My crouch script is not being able to smoothly change the camera’s local position even when the smooth damp function is applied correctly (probably :P)

Here’s the script :

#pragma strict

var player : GameObject;
var cameraObject : Transform;
var cameraCrouchHeight : float;

@HideInInspector
var playerCapsule : CapsuleCollider;
@HideInInspector
var playerOrigHeight : float;
@HideInInspector
var playerCrouchHeight : float;
@HideInInspector
var cameraOrigHeight : float;
@HideInInspector
var crouching : boolean = false;
@HideInInspector
var obstructionAbove : boolean = false;

function Start ()
{
//Getting the capsule collider component attached to the playerCapsule gameobject
playerCapsule = player.GetComponent(CapsuleCollider);

//Storing the capsule collider height
playerOrigHeight = playerCapsule.height;

playerCrouchHeight = playerCapsule.height/2;

//Storing the local height of the camera relative to the player
cameraOrigHeight = cameraObject.transform.localPosition.y;

}

function Update ()
{
if(Input.GetButtonDown(“Crouch”) && !crouching)
{
crouch();
}

else if(Input.GetButtonDown("Crouch") && crouching && !obstructionAbove)
{
	//Restoring all the original values
	playerCapsule.height = playerOrigHeight;
	playerCapsule.center.y = 0;
	cameraObject.transform.localPosition.y = cameraOrigHeight;
	crouching = false;
}

}

function crouch()
{
playerCapsule.height = playerCrouchHeight;
playerCapsule.center.y = -0.5;
cameraObject.transform.localPosition.y = cameraCrouchHeight;
crouching = true;
return;
}

//If our crouch head is hitting something then that means we cannot stand up
function OnTriggerStay()
{
obstructionAbove = true;
}

//If our crouch head is not hitting something then that means we can stand up
function OnTriggerExit()
{
obstructionAbove = false;
}

Please Help …

I got it, its working now. All I had to do was to bring the smooth damp function outside the if statement.

It’s working now. All I had to do was to take out the smooth damp function outside the if statements and that was it!!

Glad you solved your problem, but have you tried Mathf.Lerp? It is a great way to smooth out motions such as camera moves and rotations.