Search Unity

Changing size of collision box around character for slide action

Discussion in '2D' started by Fortheseen, Aug 25, 2014.

  1. Fortheseen

    Fortheseen

    Joined:
    Aug 25, 2014
    Posts:
    4
    Hey all, I am working on a running in place game and I was trying to figure out how to change the collision size shorter than usual only when holding down the down arrow key while he slides. That is the code I have been using so far. I thought it would work out if I had it set up that way for the code but when playing the game it completely ignores the change I made in the boxCollider. If I removed the bottom boxCollider it would display the character running mid-way through the ground after doing the slide. I don't know if there is a better way to do this and I am up for some suggestions. I am also new to coding.

    void FixedUpdate () {
    grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    anim.SetBool("Ground", grounded);

    BoxCollider2D boxCollider = (BoxCollider2D)GetComponent(typeof(BoxCollider2D));
    anim.SetBool("Slide", canSlide);
    if (Input.GetKey(KeyCode.DownArrow)) {
    anim.SetBool("Slide", true);

    boxCollider.size = new Vector2(1.1f, 0.8f);
    }
    boxCollider.size = new Vector2(1.1f, 1.2f);
    anim.SetFloat("vSpeed", rigidbody2D.velocity.y);

    }
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Give the player two child game objects, one containing the regular sized collider, the other with the smaller one. Disable one of them. Then your script just needs to activate one and deactivate the other.
     
  3. Fortheseen

    Fortheseen

    Joined:
    Aug 25, 2014
    Posts:
    4
    Hey, thanks for the reply and sorry for a delayed reply back. I tried adding those child game object colliders but as soon as I did my jump function stop working like I can continuously keep jumping now and the animation doesn't work at all. I found out if I remove the other box colliders it works again. It only seems to work with a box collider on the character. Is there no way to do this with just one collider or should I get a completely different jump script? I shall display the whole script and maybe it might make more sense than me explaining it:

    public class CharacterControl : MonoBehaviour {

    Animator anim;

    bool canSlide = false;

    bool grounded = false;
    public Transform groundCheck;
    float groundRadius = 0.2f;
    public LayerMask whatIsGround;
    public float jumpForce = 700f;

    void Start () {
    anim = GetComponent<Animator> ();
    }


    void FixedUpdate () {
    grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    anim.SetBool("Ground", grounded);

    anim.SetBool("Slide", canSlide);
    if (Input.GetKey(KeyCode.DownArrow)) {
    anim.SetBool("Slide", true);

    }
    anim.SetFloat("vSpeed", rigidbody2D.velocity.y);

    }

    void Update() {
    if (grounded && Input.GetKeyDown(KeyCode.UpArrow)) {
    anim.SetBool("Ground", false);
    anim.SetBool("Slide", false);
    rigidbody2D.AddForce(new Vector2(0, jumpForce));
    }

    Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
    if (screenPosition.x < 0) {
    Death();
    }

    if (Input.GetKeyDown(KeyCode.RightArrow)) {
    Death ();
    }
    }

    void On(Collider other) {
    if (other.tag == "Box") {
    Death();
    }
    }

    void Death() {
    Application.LoadLevel(Application.loadedLevel);
    }
    }