- Home /
how to resize box collider?
Hey people, i'm having trouble to resize from script a box collider.
the point is: (all in my script) i create several boxes(prefab) in different positions and then i want to assing them a collider. if i look at the inspector, they've already got one, but i've not been able to modify it. my last try was modifying some code relative to resize a Mesh, but still not working.
here's the code:
function creaBordes(x:int,z:int){
for(var i:int=0;i<4;i++){
bordes[i] = Instantiate(borde, vectores[i], Quaternion.identity);
}
asignaMeshCollider(x,z);
}
function asignaVectores(x:int,z:int){
vectores[0] = Vector3(x/2, 0, 0);
vectores[1] = Vector3(x/2, 0, z);
vectores[2] = Vector3(0, 0, z/2);
vectores[3] = Vector3(x, 0, z/2);
}
function asignaMeshCollider(x:int,z:int){
var bordeColision:BoxCollider[] = new BoxCollider[4];
bordeColision[0].size.x=x;
bordeColision[0].size.y=altura;
bordeColision[1].size.x=x;
bordeColision[1].size.y=altura;
bordeColision[2].size.z=z;
bordeColision[2].size.y=altura;
bordeColision[3].size.z=z;
bordeColision[3].size.y=altura;
for(var i:int=0;i<4;i++){
bordes[i].transform.gameObject.AddComponent(BoxCollider);
bordes[i].transform.GetComponent(BoxCollider).collider=bordeColision[i];
}
}
I am a little confused there, why do you want to use a script to assign a box collider? You can simply add it to the object and size from the inspector, in the box collider and scale X, Y and Z as well as position for it. This way also everytime you create a prefab you would have your box already done.
Is there a point for you to use your script?
Answer by aldonaletto · Dec 16, 2011 at 07:35 PM
The box collider follows the cube size - there's no need to change its collider size, unless you want the collider to be larger or smaller than the cube. Anyway, if you want to change the collider size, try this:
function asignaMeshCollider(x:int,z:int){
for(var i: int = 0; i < 4; i++){ var boxCol: BoxCollider = bordes[i].transform.collider; if (i < 2) boxCol.size.x = x; else boxCol.size.z = z; boxCol.size.y = altura; bordes[i].transform.collider = boxCol; } } NOTE: The prefab must have one and only one box collider for this to work.
I think the line "bordes[i].transform.collider = boxCol;" isn't necessary, and may cause an error.
@Blazor Ramone, you may be right: if boxCol is created as a BoxCollider reference, any changes will affect directly the object's collider, and that last assignment isn't necessary; on the other hand, if boxCol is a BoxCollider variable, it must be copied to the object's collider to have effect. It's different from the old C, where you can always know when the variable is a structure or its pointer. I'll test this.
Answer by bateras · Dec 16, 2011 at 09:00 PM
because those 4 box colliders are "surrounding" a terrain and the terrain can be resized during execution. and i need the boxes to replace and resize according to terrain's size
Answer by bateras · Dec 19, 2011 at 07:45 AM
Ok, answer is correct except that last line you've already told wouldn't be necessary. in fact, that line doesn't work because is trying to modify Read Only data.
thanks a lot.
there's an "add new comment" on the bottom right corner of an answer post...
ah yes, i'm sorry. anyway i wanted to write only one answer, but firefox got lagged and i reloaded 3 times. XD thx ;)
Answer by Varaughe · May 05, 2013 at 03:48 AM
Can anyone tell me how I can resize the box collider,in unity editor, by mouse ?
Within the Box Collider component, there should be a variable called size, change that value.
Your answer
Follow this Question
Related Questions
Adding a box collider to an object in csharp script 3 Answers
Internal collisions 1 Answer
Move Box Collider seperately from gameObject 3 Answers
Animation Script not working. 1 Answer
Audio play when object hit collision. 2 Answers