So i have a c# script for crouching,And as hard as i try to find solution to not fall trough floor i can’t.So can someone help me ?
C#
public class Crouch : MonoBehaviour
{
public AnimationCurve crchHeightAnimation = new AnimationCurve(new Keyframe[]
{ new Keyframe(0f, 0f), new Keyframe(0.5f, 0.3f), new Keyframe(1f, 1f) });
public CharacterMotor CharMotor;
public CharacterController CharCont;
private Transform _transform;
public float crouchSpeed = 1;
public float crouchHeight = 1f;
private float crouchPhase;
private float initHeight;
void Start()
{
_transform = transform;
initHeight = CharCont.height;
}
void FixedUpdate()
{
float crouchHeightOld01 = crchHeightAnimation.Evaluate(crouchPhase);
crouchPhase = Mathf.MoveTowards(crouchPhase, Input.GetKey(KeyCode.C) ? 1f : 0f, Time.fixedDeltaTime);
float crouchHeightNew01 = crchHeightAnimation.Evaluate(crouchPhase);
CharCont.height = initHeight - crouchHeightNew01 * crouchHeight;
CharCont.center = Vector3.Lerp(Vector3.zero, Vector3.up * crouchHeight * 0.5f, crouchHeight);
transform.position -= Vector3.up * (crouchHeightNew01 - crouchHeightOld01) * crouchHeight;
}
}
Any tips or help?
Thank you