Mathf.SmoothDamp problem
Hello, I'm kind of new to unity, I mean I know the basics, and I'm learning C# so sorry if this is a stupid question... I've been searching on google how to solve this and I got this far.
So I'm using Unity's standard asset FPSController, I've added the ability to crouch then I wanted to smooth the transform of the Y position on the camera, I've explored the Mathf functions, lerp, smoothdamp, smoothangle and I understand what they do. I have implemented smoothdamp on my code but I'm not getting my expected result.
Inicially the camera's Y position is 1.5 when I press the crouch button the value is changed to half (0.75) this just "teleports" the camera in the Y position.
What am I doing wrong? Can someone help me solve this?
And sorry about my english...
private void FixedUpdate()
{
float speed;
GetInput(out speed);
// always move along the camera forward as it is the direction that it being aimed at
Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
// get a normal for the surface that is being touched to move along it
RaycastHit hitInfo;
Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
m_CharacterController.height/2f);
desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
m_MoveDir.x = desiredMove.x*speed;
m_MoveDir.z = desiredMove.z*speed;
if (m_CharacterController.isGrounded)
{
m_MoveDir.y = -m_StickToGroundForce;
if (m_Jump)
{
m_MoveDir.y = m_JumpSpeed;
PlayJumpSound();
m_Jump = false;
m_Jumping = true;
}
}
else
{
m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
}
m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
ProgressStepCycle(speed);
UpdateCameraPosition(speed);
if (m_IsCrouching)
{
m_Camera.transform.localPosition = new Vector3(m_Camera.transform.localPosition.x, Mathf.SmoothDamp(m_Camera.transform.localPosition.y, m_Camera.transform.localPosition.y / 2f, ref velocity, m_CrouchSpeed * Time.fixedTime), m_Camera.transform.localPosition.z);
if (m_CharacterController.height > m_ccHeight / 2f)
{
m_CharacterController.height = m_ccHeight / 2f;
m_CharacterController.center = new Vector3(0f, m_ccCenter.y /2f, 0f);
}
}
else
{
Ray crouchRay = new Ray(transform.position + Vector3.up * m_CharacterController.radius * k_Half, Vector3.up);
float crouchRayLength = m_ccHeight - m_CharacterController.radius * k_Half;
if (Physics.SphereCast(crouchRay, m_CharacterController.radius * k_Half, crouchRayLength))
{
m_IsCrouching = true;
m_Camera.transform.localPosition = new Vector3(m_Camera.transform.localPosition.x, Mathf.SmoothDamp(m_Camera.transform.localPosition.y, m_Camera.transform.localPosition.y / 2f, ref velocity, m_CrouchSpeed * Time.fixedTime), m_Camera.transform.localPosition.z);
return;
}
Debug.DrawRay(transform.localPosition, Vector3.up, Color.red,crouchRayLength);
}
if (!m_IsCrouching)
{
m_Camera.transform.localPosition = new Vector3(m_Camera.transform.localPosition.x, Mathf.SmoothDamp(m_Camera.transform.localPosition.y, m_OriginalCameraPosition.y, ref velocity, m_StandSpeed * Time.fixedTime), m_Camera.transform.localPosition.z);
if (m_CharacterController.height < m_ccHeight)
{
m_CharacterController.height = m_ccHeight;
m_CharacterController.center = new Vector3(0f, m_ccCenter.y, 0f);
}
Ray crouchRay = new Ray(transform.position + Vector3.up * m_CharacterController.radius * k_Half, Vector3.up);
float crouchRayLength = m_ccHeight - m_CharacterController.radius * k_Half;
if (Physics.SphereCast(crouchRay, m_CharacterController.radius * k_Half, crouchRayLength))
{
m_IsCrouching = true;
m_Camera.transform.localPosition = new Vector3(m_Camera.transform.localPosition.x, Mathf.SmoothDamp(m_Camera.transform.localPosition.y, m_Camera.transform.localPosition.y / 2f, ref velocity, m_CrouchSpeed * Time.fixedTime), m_Camera.transform.localPosition.z);
}
Debug.DrawRay(transform.localPosition, Vector3.up, Color.blue, crouchRayLength);
}
Debug.LogWarning(m_Camera.transform.localPosition.y);
}