- Home /
 
My character rescales under any object
Hi, i am trying to make a simple platformer and my character is a little square, the game is going very good but there is a problem with crouching, the crouching is going fine but when i leave my crouching button (s, down) while i am under an object, The slow speed and the ability to crouch is still there but the square re-scales

Here is the code :
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
 
     public CharacterController2D controller;
 
     public float runSpeed = 40f;
     public float crouchSpeed = 30f; 
 
     bool jump = false;
     bool crouch = false;
 
     public float horizontalMove = 0f;
 
     // Update is called once per frame
     void Update()
     {
         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
 
         if (Input.GetButtonDown("Jump"))
         {
             jump = true;
         }
 
         if (Input.GetButtonDown("Crouch"))
         {
             crouch = true;
             transform.localScale = new Vector3(0.635f, 0.2559275f, 1);
         } else if (Input.GetButtonUp("Crouch"))
         {
             crouch = false;
             transform.localScale = new Vector3(0.635f, 0.613f, 1);
         }
     }
 
     void FixedUpdate ()
     {
         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
         jump = false;
     }
 
 }
 
 
              Answer by Headrush95 · Jul 30, 2020 at 12:56 AM
@Sam_da_dev you need to check if you are squished basically, to do this you need to add the blocks above to a layermask. in the inspector, top right when the above block is selected you will see a box that says "layer", create a new layer called ceiling and assign all the overhead obstacles to it.
 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PlayerMovement : MonoBehaviour
  {
  
      public CharacterController2D controller;
  
      public float runSpeed = 40f;
      public float crouchSpeed = 30f; 
  
      bool jump = false;
 
      bool crouch = false;
  
      public float horizontalMove = 0f;
 
     public bool isSquished;
     
     
     public float celiDist = 0.5f 
     
     public LayerMask celi;
     
     void Update()
     {
     isSquished = physics2D.OverlapCircle(transfrom.position, celiDist, celi);
 horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
  
          if (Input.GetButtonDown("Jump"))
          {
              jump = true;
          }
  
      // so here you would say 
     
     if (Input.GetButtonDown("Crouch"){
     
     crouch = true;
  transform.localScale = new Vector3(0.635f, 0.2559275f, 1);
     
     } else if (Input.GetButtonUp("Crouch") && !isSquished){
     
     crouch = false;
 transfrom.localScale = new Vector3(0.635f, 0.613f, 1);
     }
 
 }
 
  void FixedUpdate ()
      {
          controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
          jump = false;
      }
  
  }
 
 
 
               i've been coding for around 2 months and only on 3D games but im pretty sure this should work. please let me know
It worked, i also had to change the Z position of the square to 1, thank you for the help
Your answer