Slowing down crouch

Hi all!

I have ‘crouch’ implemented in a script, but it’s a little too quick. Is there a way to have it happen over a span of time. I’m really just beginning with all this, so the specific code I would need alludes me.

Thanks for any help you can give.

	public float crouchScaleAmount = 0.01f;
	
	private bool isCrouching;
	private float intialYSize;
	private float crouchYSize;
	
	void Awake()
	{
		isCrouching = false;
		intialYSize = transform.collider.bounds.extents.y;
	}
	
	void Update()
	{
		if (Input.GetButtonDown("crouch"))
		{
			if (isCrouching == false)
			{
				transform.localScale += new Vector3(0, -crouchScaleAmount, 0);
				crouchYSize = transform.collider.bounds.extents.y;
				
				isCrouching = true;
			}
		}
		
		if (Input.GetButtonUp("crouch"))
		{
			if (isCrouching == true)
			{
				
				transform.localScale += new Vector3(0, crouchScaleAmount, 0);
				transform.position += new Vector3(0, 0 + (intialYSize - crouchYSize), 0);
				
				isCrouching = false;
			}
		}
	}

To be fair I don’t know if changing the scale is a good approach for a crouch but it works…

using System.Collections;
using UnityEngine;

public class SmoothCrouch : MonoBehaviour {

	public float crouchScaleAmount = 0.01f;
	public float crouchDuration = 1f;

	private bool isCrouching;
	private float intialYSize;
	private float crouchYSize;

	private float initialYScale;
	private bool coroutineRunning;

	void Awake() {
		intialYSize = transform.collider.bounds.extents.y;
		initialYScale = transform.localScale.y;
	}

	void Update() {
		if (Input.GetButtonDown("crouch")) {
			if (!isCrouching) {
				if (coroutineRunning  crouchYSize != initialYScale - crouchScaleAmount) {
					StopAllCoroutines();
				}
				StartCoroutine(SmoothCrouchCoroutine(initialYScale - crouchScaleAmount, crouchDuration));
				crouchYSize = transform.collider.bounds.extents.y;
				isCrouching = true;
			}
		}

		if (Input.GetButtonUp("crouch")) {
			if (isCrouching) {
				if (coroutineRunning  crouchYSize != initialYScale) {
					StopAllCoroutines();
				}
				StartCoroutine(SmoothCrouchCoroutine(initialYScale, crouchDuration));
				// I don't know for what you're using this line but I've commented it just to test the crouch and for me it works.
				//transform.position += new Vector3(0, intialYSize - crouchYSize, 0);
				isCrouching = false;
			}
		}
	}

	private IEnumerator SmoothCrouchCoroutine(float desiredValue, float duration) {
		crouchYSize = transform.localScale.y;
		for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / duration) {
			coroutineRunning = true;
			float temp = Mathf.Lerp(crouchYSize, desiredValue, t);
			transform.localScale = new Vector3(0, temp, 0);
			yield return null;
		}
		coroutineRunning = false;
	}
}

I’ve just added a Lerp function to smooth the crouch operation.

transform.localScale = new Vector3(0, temp, 0);

should be be:

transform.localScale = new Vector3(transform.localScale.x, temp, transform.localScale.z);

:wink:

oh yes !!!
sorry, you’re totally right :slight_smile: