Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Warhulk1745 · Jan 06, 2016 at 12:47 AM · collisionobjectvector3detection

Detect if an object is bigger or smaller than you when you collide with it.

Hello everyone !

I need help with a script that basically detects if the object the player collides with is bigger or smaller than the player.

If the object is bigger, the player dies. if the object is smaller, the player eats it.

C# is what I'm working with, please guys any help would really help me out.

Comment
Add comment · Show 2
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 brunocoimbra · Jan 06, 2016 at 01:10 AM 0
Share

Is that a 2D or 3D project?

avatar image Warhulk1745 brunocoimbra · Jan 06, 2016 at 01:15 AM 0
Share

It's a 3D project.

@brunocoimbra

1 Reply

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

Answer by brunocoimbra · Jan 06, 2016 at 01:53 AM

Assuming you are using boxColliders, you can use:

 private BoxCollider boxCollider;
 private float volume;
 
 void Start()
 {
     boxCollider = GetComponent<BoxCollider >();
     Vector3 geometry = boxCollider.bounds.size;
     volume = geometry.x * geometry.y * geometry.z;
 }
 
 void OnTriggerEnter(Collider otherCollider)
 {
     Vector3 geometry = otherCollider.bounds.size;
     float otherVolume = geometry.x * geometry.y * geometry.z;
     if (volume < otherVolume)
     {
         // die;
     }
     else if (volume > otherVolume)
     {
         // eat;
     }
 }

There is ways to calculate volume for spheres and cillinders (and other shapes) too, google is your friend here. Another option would be play around with the Collider.bounds to see what fits to you.

Also, the Mesh component also has a bounds.size, but I would say to you stick with colliders, as handling collisions will be easier.

Comment
Add comment · Show 3 · 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 Warhulk1745 · Jan 06, 2016 at 02:43 AM 0
Share

@ brunocroimbra

Thank you so much for the fast response ! Hey I hate to be a pain, but there seems to be something wrong with the code you submitted. I'm getting errors . =(

Error # 1: Assets/Scripts/sizeDetect.cs(12,9): error CS0266: Cannot implicitly convert type UnityEngine.Collider' to UnityEngine.BoxCollider'. An explicit conversion exists (are you missing a cast?)

Error # 2:

Assets/Scripts/sizeDetect.cs(13,40): error CS1061: Type UnityEngine.BoxCollider' does not contain a definition for Bounds' and no extension method Bounds' of type UnityEngine.BoxCollider' could be found (are you missing a using directive or an assembly reference?)

Error # 3:

Assets/Scripts/sizeDetect.cs(19,42): error CS1061: Type UnityEngine.Collider' does not contain a definition for Bounds' and no extension method Bounds' of type UnityEngine.Collider' could be found (are you missing a using directive or an assembly reference?)

avatar image brunocoimbra Warhulk1745 · Jan 06, 2016 at 03:09 AM 0
Share

Sorry about that, I didn't tested the code, but already edited my answer with the corrections.

The Error#1 was because of "GetComponent()" that should be "GetComponent()". The Error#2 and Error#3 was because "Bounds" were meant to be "bounds" (Bounds is the type).

avatar image Warhulk1745 brunocoimbra · Jan 06, 2016 at 03:41 AM 1
Share

Thank you so much for your help ! This will work for me, I'll be able to build on to this to fit my game ! =) Thank you brunocoimbra !

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

49 People are following this question.

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

Related Questions

Check for collision ONCE 0 Answers

Need some help programming basic AI movement 0 Answers

Get list of enemies in range 1 Answer

how to destroy an object once you collide with it the second time? 0 Answers

Moving an object to another object code help 1 Answer


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