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

Changing Capsule Collider 2D properties (size and offset) works in script 01, but not in script 02

Discussion in '2D' started by kylebengzon, Feb 23, 2020.

  1. kylebengzon

    kylebengzon

    Joined:
    Sep 26, 2019
    Posts:
    11
    Hi everyone,

    As the title suggests, I have two scripts: PlayerMovement and CharacterController2D, and both are trying to change the size and offset of my capsule collider under their respective circumstances. The code I used:

    Vector2 colliderCrouchSize;
    Vector2 colliderCrouchOffset;
    ...
    void Start()
    {
    bodyCollider = GetComponent<CapsuleCollider2D>();
    ...
    colliderCrouchSize = new Vector2(bodyCollider.size.x, bodyCollider.size.y / 2f);
    colliderCrouchOffset = new Vector2(bodyCollider.offset.x, bodyCollider.offset.y * 2.005f);
    }
    ...
    Function ()
    {
    ...
    bodyCollider.size = colliderCrouchSize;
    bodyCollider.offset = colliderCrouchOffset;
    ...
    }


    Works perfectly inside the PlayerMovement script, but when I copy-pasted the code to fit into the CharacterController2D, with the proper name changes so I can differentiate the two, it does not execute.

    I can't understand why the same code won't work in a different script. I'm pasting a trimmed version of the CharacterController2D script to include only the collider related variables:

    ====================================================================================

    public class CharacterController2D : MonoBehaviour
    {
    ...
    CapsuleCollider2D capsuleCollider;
    ...
    Vector2 colliderStandSize;
    Vector2 colliderStandOffset;

    Vector2 colliderCrouchSize;
    Vector2 colliderCrouchOffset;

    ...

    private void Start()
    {
    capsuleCollider = GetComponent<CapsuleCollider2D>();

    colliderStandSize = capsuleCollider.size;
    colliderStandOffset = capsuleCollider.offset;

    colliderCrouchSize = new Vector2(capsuleCollider.size.x, capsuleCollider.size.y / 2f);
    colliderCrouchOffset = new Vector2(capsuleCollider.offset.x, capsuleCollider.offset.y * 2.005f);

    }
    ...

    private void FixedUpdate()
    {
    ...
    }

    public void Move(float move, bool crouch, bool jump)
    {
    ...

    //only control the player if grounded or airControl is turned on
    if (m_Grounded || m_AirControl)
    {
    if (crouch)
    {
    ...
    //Trying to apply the crouching collider size and offset
    capsuleCollider.size = colliderCrouchSize;
    capsuleCollider.offset = colliderCrouchCenter;

    }
    else
    {
    ...
    //Trying to return to standing capsule data
    capsuleCollider.size = colliderStandSize;
    capsuleCollider.offset = colliderStandCenter;

    }
    ...
    }
    ...
    }
    ...
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please use code-tags when you post code otherwise it's very hard to follow code or mistakes.

    It's there on the toolbar when editing.
     
  3. kylebengzon

    kylebengzon

    Joined:
    Sep 26, 2019
    Posts:
    11
    Hi Melv,

    Thanks for telling me about code-tags. I'm sorry for the inconvenience. I hope it comes out easier to read with this reply.

    This one works:

    Code (CSharp):
    1. Vector2 colliderCrouchSize;
    2. Vector2 colliderCrouchOffset;
    3. ...
    4. void Start()
    5. {
    6. bodyCollider = GetComponent<CapsuleCollider2D>();
    7. ...
    8. colliderCrouchSize = new Vector2(bodyCollider.size.x, bodyCollider.size.y / 2f);
    9. colliderCrouchOffset = new Vector2(bodyCollider.offset.x, bodyCollider.offset.y * 2.005f);
    10. }
    11. ...
    12. Function ()
    13. {
    14. ...
    15. bodyCollider.size = colliderCrouchSize;
    16. bodyCollider.offset = colliderCrouchOffset;
    17. ...
    18. }
    This one does not work:

    Code (CSharp):
    1. public class CharacterController2D : MonoBehaviour
    2. {
    3. ...
    4. CapsuleCollider2D capsuleCollider;
    5. ...
    6. Vector2 colliderStandSize;
    7. Vector2 colliderStandOffset;
    8.  
    9. Vector2 colliderCrouchSize;
    10. Vector2 colliderCrouchOffset;
    11. ...
    12.  
    13. private void Start()
    14. {
    15. capsuleCollider = GetComponent<CapsuleCollider2D>();
    16.  
    17. colliderStandSize = capsuleCollider.size;
    18. colliderStandOffset = capsuleCollider.offset;
    19.  
    20. colliderCrouchSize = new Vector2(capsuleCollider.size.x, capsuleCollider.size.y / 2f);
    21. colliderCrouchOffset = new Vector2(capsuleCollider.offset.x, capsuleCollider.offset.y * 2.005f);
    22. }
    23. ...
    24.  
    25. private void FixedUpdate()
    26. {
    27. ...
    28. }
    29.  
    30. public void Move(float move, bool crouch, bool jump)
    31. {
    32. ...
    33.  
    34. //only control the player if grounded or airControl is turned on
    35. if (m_Grounded || m_AirControl)
    36. {
    37. if (crouch)
    38. {
    39. ...
    40. //Trying to apply the crouching collider size and offset
    41. capsuleCollider.size = colliderCrouchSize;
    42. capsuleCollider.offset = colliderCrouchCenter;
    43. }
    44. else
    45. {
    46. ...
    47. //Trying to return to standing capsule data
    48. capsuleCollider.size = colliderStandSize;
    49. capsuleCollider.offset = colliderStandCenter;
    50. }
    51. ...
    52. }
    53. ...
    54. }
    55. ...
    56. }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    It's easier to read but TBH there's no description of what exactly is not working (are the expected values not correct?) and the code looks a little like pseudo code with too much left out. Pretty much impossible to tell you what's wrong.

    You must know what the values are you want for size, offset etc which I guess are what's important here so I would suggest that you place a breakpoint, debug and single-step to figure out the values between the two after all it's all in front of you. It's an essential skill and quicker than trying to do bare comparisons between source files.

    Sorry I couldn't help further.
     
  5. kylebengzon

    kylebengzon

    Joined:
    Sep 26, 2019
    Posts:
    11
    I'm also sorry that I'm not being very clear. To try and give more context, I posted in another thread about an OverlapCapsule and you helped me get that sorted out (Thank you very much for that).

    What I'm (still) trying to do is get my player to crouch when a ceiling is detected overhead. So far, all the other codes that are related to crouching (animations, slowed movement, etc) are executing just fine, except for this "change capsule size from standing to crouching".

    As I mentioned, I am able to change the capsule size in the PlayerMovement script and thought, "I simply need to copy paste the same code into CharacterController2D". Also, the script isn't detecting any errors, and I can go into play mode in unity.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    I can understand what you're trying to do but there's not enough info sort out your issue. The main thing I was getting to was that you should use the debugging tools provided to figure out what's going wrong. In the end, not all issues can be solved by posting bits of code.

    In the end, you need to confirm that you'er provided the correct info to Unity and debugging is an essential part of that.
     
  7. kylebengzon

    kylebengzon

    Joined:
    Sep 26, 2019
    Posts:
    11
    Alright. I'll try to debug as you suggested.

    Thanks Melv!
     
  8. kylebengzon

    kylebengzon

    Joined:
    Sep 26, 2019
    Posts:
    11
    Hi Everyone,

    I just wanted to give an update on my issue:

    I managed to accomplish making the player stay crouched underneath a platform.

    (I think) The reason it wasn't working before is the same code was being executed from the 2 different scripts, and one was overriding the other. I negated the code from PlayerMovement and left it in CharacterController2D and everything works fine.