Hi, currently I’m using a simple script to change the size of a 2d object, which has a rigidbody2d and boxcollider2d attached. Here’s the script:
public float growspeed = 0.75f;
float scale = 0;
public float duration = 5f;
BoxCollider2D collider;
// Use this for initialization
void Start () {
collider = GetComponent<BoxCollider2D> ();
}
// Update is called once per frame
void Update () {
duration -= Time.deltaTime;
scale += growspeed * Time.deltaTime;
transform.localScale = new Vector3 (scale, transform.localScale.y, 0);
if (scale > 1) {
collider.size = new Vector2 (scale, collider.size.y);
}
if (duration <= 0) {
Destroy (this.gameObject);
}
}
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.tag == "Player") {
Destroy (this.gameObject);
}
As you can see in the script, when I change the size of the object I have to also change the size of the box collider to maintain collision detection - however, when I change the size of the box collider as well, it causes the object to randomly change size and move its position downwards - effectively it kind of distorts. If I don’t change the size of the boxcollider, then the object smoothly and normally changes size. How would I be able to fix this? Any help would be appreciated.