- Home /
What's wrong with my Script? Changing Collision
I want the collision to change when the character ducks/squats and it DOES. But I also want the collision to change when the character jumps. I currently tried this, but it doesn't work for some reason. Does anybody know why? Thanks.
function Update () {
var colliderBox = gameObject.GetComponent(BoxCollider); //controlls collider box size
var AT = gameObject.GetComponent(AnimateTexture); //Store AnimateTexture Script
if(Input.GetKey("space")){
AT.rowNumber = 3; //Player jumping animation
colliderBox.center = Vector3(0.03, -0.01, 0);
colliderBox.size = Vector3(10, 6, 1);
}else{
colliderBox.center = Vector3(0.03, -0.29, 0);
colliderBox.size = Vector3(0.44, 0.37, 1);
}
if(Input.GetKey("s")){
AT.rowNumber = 4; //Player squats down
colliderBox.center = Vector3(0.03, -0.38, 0);
colliderBox.size = Vector3(0.44, 0.16, 1);
}else{
colliderBox.center = Vector3(0.03, -0.29, 0);
colliderBox.size = Vector3(0.44, 0.37, 1);
}
}
Answer by EvilWarren · Oct 28, 2013 at 11:44 AM
Your collider values look strange but I'm just going to assume you know what you're doing with them. The problem is your second if statement wipes out any values you assigned in the first statement, as one of the tow conditions in the second will always be executed.
Try this
function Update () {
var colliderBox = gameObject.GetComponent(BoxCollider); //controlls collider box size
var AT = gameObject.GetComponent(AnimateTexture); //Store AnimateTexture Script
if(Input.GetKey("space")){
AT.rowNumber = 3; //Player jumping animation
colliderBox.center = Vector3(0.03, -0.01, 0);
colliderBox.size = Vector3(10, 6, 1);
}else if(Input.GetKey("s")){
AT.rowNumber = 4; //Player squats down
colliderBox.center = Vector3(0.03, -0.38, 0);
colliderBox.size = Vector3(0.44, 0.16, 1);
}else{
colliderBox.center = Vector3(0.03, -0.29, 0);
colliderBox.size = Vector3(0.44, 0.37, 1);
}
}
if, else if, else. I gotta remember that pattern.
And yes, my values are ridiculous because I was trying to see in the game preview window the difference in size of the boxcollider. So I gave some astronomical number lol. Forgot to change it to something more realistic before posting. But all that aside, THAN$$anonymous$$ YOU!!!! I believe I tried something similar, but because of my insane number values, the collision grew large enough to hit my enemy and the player was dying immediately, and I didn't know why haha. So I'd also like to thank you for mentioning my ridiculous collision values, too. I probably would've overlooked it otherwise, and continued wondering what was going on lol. Thanks! I'd thumb your comment, but I don't have the rep for that yet.