Resizing Box Collider Messing with Rigidbody Collisions
I am making a game where I constantly need the Box Collider to resize to fit the sprite size. I have used a script and the SpriteRenderer to change the sprite and I've resized the box collider as I do that, but when it's resized, it allows the player to fall through the floor and go through the walls.
My script:
void Start(){
StartCoroutine (anim (timeBetweenSprites));
}
private void setBoxColliderSize(float x, float y, float z){
GetComponent<BoxCollider> ().size = new Vector3 (x, y, z);
// 2.9f, 2.89f, 0.2f for walk1
// 1.69f, 2.89f, 0.2f for idle
// 1.42f, 2.89f, 0.2f for walk2
}
void FixedUpdate () {
if (Input.GetKey (KeyCode.D)) {
transform.position = new Vector3 (transform.position.x + moveSpeed, transform.position.y, transform.position.z);
if (isLeft) {
isLeft = false;
firstFrame = true;
}
}
if (Input.GetKey (KeyCode.A)) {
transform.position = new Vector3 (transform.position.x - moveSpeed, transform.position.y, transform.position.z);
if (!isLeft) {
isLeft = true;
firstFrame = true;
}
}
if(Input.GetKeyDown(KeyCode.D)){
isIdle = false;
}
if(Input.GetKeyDown(KeyCode.A)){
isIdle = false;
}
if(Input.GetKeyUp(KeyCode.D)){
GetComponent<SpriteRenderer>().sprite = rightIdle;
setBoxColliderSize (1.69f, 2.89f, 0.2f);
isIdle = true;
}
if(Input.GetKeyUp(KeyCode.A)){
GetComponent<SpriteRenderer>().sprite = leftIdle;
setBoxColliderSize (1.69f, 2.89f, 0.2f);
isIdle = true;
}
if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.W)) {
if (firstJump && !secondJump) {
GetComponent<Rigidbody> ().AddForce (Vector3.up * (jumpForce/2));
secondJump = true;
}
if (!firstJump) {
GetComponent<Rigidbody> ().AddForce (Vector3.up * jumpForce);
firstJump = true;
}
}
}
void Update(){
if (isTouchingWall) {
if (isLeft) {
GetComponent<SpriteRenderer> ().sprite = leftWalk1;
setBoxColliderSize (2.9f, 2.89f, 0.2f);
} else {
GetComponent<SpriteRenderer> ().sprite = rightWalk1;
setBoxColliderSize (2.9f, 2.89f, 0.2f);
}
}
if (firstFrame) {
if (isLeft) {
GetComponent<SpriteRenderer> ().sprite = leftWalk1;
setBoxColliderSize (2.9f, 2.89f, 0.2f);
firstFrame = false;
} else {
GetComponent<SpriteRenderer> ().sprite = rightWalk1;
setBoxColliderSize (2.9f, 2.89f, 0.2f);
firstFrame = false;
}
}
}
IEnumerator anim(float time){
yield return new WaitForSeconds (time);
if (isTouchingWall) {
if (isLeft) {
GetComponent<SpriteRenderer> ().sprite = leftWalk1;
setBoxColliderSize (2.9f, 2.89f, 0.2f);
} else {
GetComponent<SpriteRenderer> ().sprite = rightWalk1;
setBoxColliderSize (2.9f, 2.89f, 0.2f);
}
} else {
if (!isIdle) {
if (isLeft && animFrame == 0) {
animFrame = 1;
GetComponent<SpriteRenderer> ().sprite = leftWalk1;
setBoxColliderSize (2.9f, 2.89f, 0.2f);
} else if (isLeft && animFrame == 1) {
animFrame = 0;
GetComponent<SpriteRenderer> ().sprite = leftWalk2;
setBoxColliderSize (1.42f, 2.89f, 0.2f);
} else if (!isLeft && animFrame == 0) {
animFrame = 1;
GetComponent<SpriteRenderer> ().sprite = rightWalk1;
setBoxColliderSize (2.9f, 2.89f, 0.2f);
} else {
animFrame = 0;
GetComponent<SpriteRenderer> ().sprite = rightWalk2;
setBoxColliderSize (1.42f, 2.89f, 0.2f);
}
}
}
StartCoroutine (anim (timeBetweenSprites));
}
void OnCollisionEnter(Collision c){
if (c.gameObject.name.Equals ("Ground")) {
firstJump = false;
secondJump = false;
}
if (c.gameObject.name.Equals ("LeftWall") || c.gameObject.name.Equals ("RightWall")) {
isTouchingWall = true;
}
}
void OnCollisionExit(Collision c){
if (c.gameObject.name.Equals ("LeftWall") || c.gameObject.name.Equals ("RightWall")) {
isTouchingWall = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
How to assign GameObject to Script? 1 Answer
I can't add the script to fix my broken script, because my script is broken 1 Answer
How can I extend an UI Component and change it's default values when added on editor? 0 Answers
Loading a scene with script loads multiple copies of another scene, the crashes Unity 0 Answers