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 /
avatar image
0
Question by GuyFawkes · Sep 10, 2011 at 12:30 AM · collision

Simple help with my jackhammer script

I'm trying to make a jackhammer with a rock. I currently have this script. The problem I'm having is that the variable, rockhits, starts going up after the jackhammer stops colliding with the rock. What I'm trying to do is whenever the jackhammer is colliding with the rock, the variable rockhits goes up, and whenever the jackhammer stops colliding the variable stops going up. Here's my script right now:

 var collision = false;
 var rockhits = 0;

 function OnTriggerEnter(col: Collider)
 {
     if (col.transform.tag == "Rocks")
     {
         collision = true;
     }
     else
         collision = false;
         
     while (collision == true)
     {
         rockhits++;
         if (rockhits == 10)
         {
             rocks.renderer.enabled = false;
         }
         yield WaitForSeconds(0.5);
     }
 }

I know the colliding part works, I've tested that. I can't figure out why the other parts don't work.

EDIT: How come I have to completely put the jackhammer into the rock for it to detect it? (Obviously the collider is set to "is trigger" which is why I can stick it through in the first place)

Comment
Add comment
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

3 Replies

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

Answer by DaveA · Sep 10, 2011 at 12:41 AM

 while (collision == true)

What do you expect?

Look at OnCollisionExit and OnCollisionStay. Probably the latter for you. Or you can increment that count in an Update function, if you use a boolean to indicate that it has 'entered' (in On..Enter) and 'un-entered' (in On..Exit).

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 GuyFawkes · Sep 10, 2011 at 12:47 AM 0
Share

OnTriggerExit worked for me. I hadn't even though about that! Thanks! One thing though, how come I have to completely put the jackhammer into the rock for it to detect it? (Obviously the collider is set to "is trigger" which is why I can stick it through in the first place)

avatar image
1

Answer by Adam-Buckner · Sep 10, 2011 at 01:10 AM

You don't need this to be a trigger. You could make this a collider. There is an analagous OnCollider family (no urls this time) that behaves very similarly. You can make the collider solid and OnCollisionEnter flag true and OnCollisionExit flag false, or try OnCollisionStay to increment your count.

My only words of caution would be you might want a time counter rather than a hit counter as you'd be getting hits per frame and this is unreliable. If you had OnCollisionStay increment a counter by Time.deltaTime and if the counter was greater than the time threshold, kill the rock, this might be more accurate.

Lastly, you will need some way of detecting which rock you are breaking, so there would need to be either an id per rock and a list or array of rocks being broken, or the rock itself should carry the counter so he rock knows and holds its own damage.

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 GuyFawkes · Sep 10, 2011 at 01:21 AM 0
Share

The reason I want it to be a trigger is so the mesh doesn't collide with the character that's holding it. So what's my problem though?

avatar image Adam-Buckner ♦♦ · Sep 10, 2011 at 10:16 AM 0
Share

So, you're worried about the collider on the jackhammer touching the player?

You could do a few things.

One would to to have an "intert" collider on the jackhammer to prevent it from going thru the rock and a trigger collider on the tip to do the rock breaking detection.

Another would be to look into "ignore collision:

http://unity3d.com/support/documentation/ScriptReference/Physics.IgnoreCollision.html

or "ignore collision layer"

http://unity3d.com/support/documentation/ScriptReference/Physics.IgnoreLayerCollision.html

avatar image GuyFawkes · Sep 10, 2011 at 06:32 PM 0
Share

I actually got it work by changing the center of the box collider to prevent my problem. Thanks for your help!

avatar image
0

Answer by Adam-Buckner · Sep 10, 2011 at 12:45 AM

In OnTriggerEnter() you turn on your while loop if the tag is "Rocks".

You don't have an option of turning it off again until you touch another collider that is NOT "Rocks".

All this works in OnTriggerEnter as well.

You need a way to actively turn OFF your while loop when you are not touching your "Rocks" object.

You should look into OnTriggerExit and OnTriggerStay http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerExit.html http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerStay.html

You may want to look into putting your while loop into its own method that is called by OnTriggerEnter for more control, but more likely look at using OnTriggerStay().

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 Adam-Buckner ♦♦ · Sep 10, 2011 at 12:46 AM 1
Share

Ratz, beat out by getting the urls...

avatar image GuyFawkes · Sep 10, 2011 at 12:58 AM 0
Share

I already found a solution from DaveA. But maybe you could answer my question I just edited in? (By the way thanks for answering anyways! I appreciate it)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Physics2D.OverlapAreaAll/NoAlloc fails detecting object in puzzling manner 1 Answer

Weird collision error when using waypoint script... 1 Answer

Movement around a huge object by avoiding obstacles 1 Answer

[Solved] Is it okay if we change transform.position on a kinematic rigid body which also does not use Gravity? 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