Question by
feygon · Oct 28, 2018 at 07:54 PM ·
c#2d-platformercolliderscapsulecollidercapsule collider
Can't set CapsuleCollider2D size at runtime
I'm working on a crouch mechanic for a 2D platformer in C#. When the sprite crouches, the size is supposed to decrease y by half, and the yoffset by 1/4. But neither of these elements is updating. Near as I can tell, it just skips over these lines of code, because I've placed console loggers, and they are reached and report no change in the data. Here is my code:
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
// check for crouching first, then jumping.
if (Input.GetButtonDown("Crouch"))
{
Debug.Log("Crouching.");
crouch = true;
animator.SetBool("IsCrouching", true);
Debug.Log("Crouching animation changed. Resetting capsule.");
float floatx1 = 0f;
float floaty1 = -.275f;
float floatx2 = .5f;
float floaty2 = .55f;
Vector2 offset = new Vector2(floatx1, -floaty1);
Vector2 size = new Vector2(floatx2, floaty2);
amayaCapsule.offset.Set(offset.x, offset.y);
amayaCapsule.size.Set(size.x, size.y);
Debug.Log("Now past offset/size mutation. yOffset is " + amayaCapsule.offset.y + " and ysize is " + amayaCapsule.size.y + ".");
}
else
{
if (Input.GetButtonUp("Crouch"))
{
Debug.Log("Standing.");
crouch = false;
animator.SetBool("IsCrouching", false);
Debug.Log("Standing animation changed. Resetting capsule.");
float floatx1 = 0f;
float floaty1 = 0f;
float floatx2 = .5f;
float floaty2 = 1.1f;
Vector2 offset = new Vector2(floatx1, floaty1);
Vector2 size = new Vector2(floatx2, floaty2);
amayaCapsule.offset.Set(offset.x, offset.y);
amayaCapsule.size.Set(size.x, size.y);
Debug.Log("Now past offset/size mutation. yOffset is " + amayaCapsule.offset.y + " and ysize is " + amayaCapsule.size.y + ".");
}
...
Here is the console readout:
console-readout.jpg
(59.4 kB)
Comment