Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by bateras · Dec 16, 2011 at 12:28 PM · colliderboxresize

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];
 }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Dec 16, 2011 at 02:55 PM 1
Share

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?

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blazor Ramone · Dec 16, 2011 at 07:40 PM 1
Share

I think the line "bordes[i].transform.collider = boxCol;" isn't necessary, and may cause an error.

avatar image aldonaletto · Dec 16, 2011 at 08:31 PM 0
Share

@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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Dec 19, 2011 at 08:03 AM 1
Share

there's an "add new comment" on the bottom right corner of an answer post...

avatar image bateras · Dec 19, 2011 at 09:28 AM 1
Share

ah yes, i'm sorry. anyway i wanted to write only one answer, but firefox got lagged and i reloaded 3 times. XD thx ;)

avatar image
0

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 ?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Artemis_Toh · Dec 30, 2015 at 07:17 AM 0
Share

Within the Box Collider component, there should be a variable called size, change that value.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges