problem with crouch speed

so im making a 2d platformer where you can crouch and it decreases your size but it seems to also decrease my speed.This is the script i have at the moment.

using UnityEngine;
using System.Collections;

public class crouch : MonoBehaviour
{

private GameObject smashRadius;
float speed = 10f;
GameObject radius;

void Update()
{
    if (Input.GetKey(KeyCode.S ))
    {
        gameObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    }
    else
    {
        gameObject.transform.localScale = new Vector3(1, 1,  1);
    }
}

}

Well, we need more information about the usage of the ‘speed’ value itself, to help you correctly. But have you tried ‘limiting’ the velocity by applying some kind of a limiter?

if (Input.GetKey (KeyCode.A) && R2D.velocity.x > -maxXVeclocity) {

With the velocity limited, no matter the GameObject’s size, its max speed does not change. The acceleration speed may vary since the change in size may end up in different friction force or etc, but its max speed won’t change this way. If you were using a ‘Transform.Translate’ function, since we all know that there’s no way that a GameObject’s size can interfere with its translation speed, go and check for every single line interfering with the ‘speed’ variable of yours.
Oh, and this is the code that I used for testing the velocity limitation, just in case you need it.

    public Rigidbody2D R2D;

	public float force;

	public float maxXVeclocity;

	public Vector3 inCrouch;
	public Vector3 afterCrouch;

	// Use this for initialization
	void Start () {
		R2D = gameObject.GetComponent<Rigidbody2D> ();
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetKey (KeyCode.A) && R2D.velocity.x > -maxXVeclocity) {
			R2D.AddForce (Vector2.left * force);
		}
		if (Input.GetKey (KeyCode.D) && R2D.velocity.x < maxXVeclocity) {
			R2D.AddForce (Vector2.right * force);
		}

		if (Input.GetKey (KeyCode.S)) {
			gameObject.transform.localScale =inCrouch;
		} else {
			gameObject.transform.localScale = afterCrouch;
		}

		//Debug.Log (R2D.velocity.x);

	}

	// Keith Rek, 2018-07-26.

But in case you would not want any limitations in the velocity, here’s the one with the limiter removed, but with a force multiplier that will be applied to the force value when the character crouches. This way, the character will receive much greater force when he/she is crouching.

    public Rigidbody2D R2D;

	public float force;
	public float forceMultiplier;
	public float forceMultiplied;

	public float maxXVeclocity;

	public Vector3 inCrouch;
	public Vector3 afterCrouch;

	// Use this for initialization
	void Start () {
		forceMultiplied = force * forceMultiplier;
		R2D = gameObject.GetComponent<Rigidbody2D> ();
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetKey (KeyCode.A)) {
			R2D.AddForce (Vector2.left * force);
		}
		if (Input.GetKey (KeyCode.D)) {
			R2D.AddForce (Vector2.right * force);
		}

		if (Input.GetKey (KeyCode.S)) {
			gameObject.transform.localScale = inCrouch;
			force = forceMultiplied;
		} else {
			gameObject.transform.localScale = afterCrouch;
			force = forceMultiplied/forceMultiplier;
		}

		//Debug.Log (R2D.velocity.x);

	}

	// Keith Rek, 2018-07-26.

The rest is up to you.

I dont know for sure what’s wrong but i can help you. Most basic is to double the speed or just leave it like that (I am pretty sure people walk slower while crouching) . You could also fix that if your character size is 1 x 1 not to change x and z values because that will cause some glitches later. Plus using vector3 in 2d is harder. It is easier to use vector2 . It’s not much, it’s just less writing .

Hey,

try gameObject.transform.localScale -= new Vector3(0.5f, 0.5f, 0.5f);

Notice the inclusion of the minus symbol before the equals symbol. Source = Unity - Scripting API: Transform.localScale