Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question "lowering" BoxCollider2D when crouching

Discussion in 'Scripting' started by pokto64, Jun 19, 2020.

  1. pokto64

    pokto64

    Joined:
    Jun 19, 2020
    Posts:
    2
    i want to "lower" the hitbox of my character when i crouch so that i can crouch under a Platfform but i dont know how to code it (im new to unity)

    edit: i prefer code

    my code until now:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     public CharacterController2D controller;
    9.     public Animator animator;
    10.     public BoxCollider2D collider;
    11.  
    12.     public float runSpeed = 40f;
    13.  
    14.     float horizontalMove = 0f;
    15.     bool jump = false;
    16.     bool crouch = false;
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    23.  
    24.         animator.SetFloat("speed", Mathf.Abs(horizontalMove));
    25.  
    26.         if (Input.GetButtonDown("Jump"))
    27.         {
    28.             jump = true;
    29.             animator.SetBool("jumping", true);    
    30.         }
    31.  
    32.  
    33.         if (Input.GetButtonDown("Crouch"))
    34.         {
    35.             crouch = true;
    36.             animator.SetBool("Crouch", true);
    37.      
    38.         }
    39.         else if (Input.GetButtonUp("Crouch"))
    40.         {
    41.             crouch = false;
    42.             animator.SetBool("Crouch", false);
    43.         }
    44.      
    45.      
    46.     }
    47.  
    48.  
    49.     public void OnLanding()
    50.     {
    51.         animator.SetBool("jumping", false);
    52.     }
    53.      
    54.         void FixedUpdate()
    55.     {
    56.      
    57.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    58.         jump = false;
    59.     }
    60.  
    61.  
    62.  
    63. }
     
    Last edited: Jun 19, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    I would recommend putting two BoxCollider2Ds on the same object and drag each specific one into public fields in the script as the "standing collider" and the "crouching collider," then just .enable the correct one based on standing/crouching. Once you do this the code doesn't have to be changed every time you change the art; you just change the collider to match the art.
     
    PraetorBlue and pokto64 like this.
  3. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    Kurt-Dekker's idea works as well, but if you really want to code your way out of this, basically BoxCollider2D has the property of interest "size", which controls the rectangle width and heigth of the 2D collider as Vector2. Now, I'll consider that "crouch" in this case reduces the sprite size by half, so basically:

    Code (CSharp):
    1. private float y_crouch, x_initial, y_initial;
    2. public BoxCollider2D collider;
    3.  
    4. private void Start()
    5. {
    6.    x_initial = collider.size.x;
    7.    y_initial = collider.size.y;
    8.    y_crouch = y_initial/2;
    9. }
    10.  
    11. private void Update()
    12. {
    13.    if (Input.GetKeyDown("Crouch"))
    14.    {
    15.       collider.size = New Vector2(x_initial,y_crouch);
    16.    }
    17.    if (Input.GetKeyUp("Crouch"))
    18.    {
    19.       collider.size  = New Vector2(x_initial, y_initial);
    20.    }
    21. }
    I haven't tested it, so sorry in advance if it doesn't work.
     
    pokto64 likes this.
  4. pokto64

    pokto64

    Joined:
    Jun 19, 2020
    Posts:
    2
    thank you but sadly it is either not working or i am to stupid
     
  5. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    Ok, change the void Update for this:
    Code (CSharp):
    1. private void Update()
    2. {
    3.    if (Input.GetKey("Crouch"))
    4.    {
    5.       collider.size = New Vector2(x_initial,y_crouch);
    6.    }
    7.    if (Input.GetKeyUp("Crouch"))
    8.    {
    9.       collider.size  = New Vector2(x_initial, y_initial);
    10.    }
    11. }
    An important thing to notice though is that with this code your collider decreses by half in the 'y' axis, so this is probably an incomplete solution I'm giving you.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Changing the vertical size will also reduce the bottom area, unless you change the offiset.

    This is precisely why I suggested an art-side fix. You know your sprite is likely to change in the future, you might want to change its crouch volume visually.

    But, whatever floats your boat.
     
    busterlock likes this.