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 Topthink · Dec 12, 2016 at 02:25 AM · collisionmovementcolliderdamage

How do I count Hits ??

I created a small terrain with high walls and placed a long spinning cube in the center. Occasionally, a colored sphere will randomly spawn in one of the corners on the terrain and then the sphere will move slowly to the center. When the spinner hits them, they bounce off the walls and each other and occasionally get hit over the wall and fall to their death...I destroy them if they get too far below the terrain. No problem with any of this, it all works fine.

BUT...

I want to destroy them a second way. If the spinner hits any of the spheres, say, five times, I want the sphere to be destroyed...sort of like taking damage but based on the number of hits rather than amount of damage. I've been researching this and I don't begin to know how to count when one of my spheres gets hit five times. I've been trying all sorts of stuff with my colliders and so forth, no luck.

Here's how I generate the spheres in one of the corners if that helps (I simply plug this into one of a switch statement...once case for each corner and one case for add nothing:

         sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
         sphere.transform.position = new Vector3 (27f, .5f, 27f);
         sphere.GetComponent<Renderer> ().material.color = Color.green;
         sphere.AddComponent<Rigidbody> ();
         sphere.AddComponent<Movement> ();

Here's my movement control:

 Vector3 goToHere = new Vector3 (15,0.5f,15);
 float speed = 3f;

 void FixedUpdate () 
 {
     transform.LookAt(goToHere);
     GetComponent<Rigidbody> ().AddForce (transform.forward*speed);
 }
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 Topthink · Dec 13, 2016 at 09:23 PM 0
Share

Okay, I figured it out.

2 Replies

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

Answer by Topthink · Dec 13, 2016 at 04:31 AM

I'm pretty sure this is exactly what I need but I'm having a hard time working out all the details. I've been looking up examples of this and trying to sort through them but I still have some questions.

It looks like a standard function but I'm not sure how it gets called...does it need to be invoked/called from "Update" or "FixedUpdate" or is this automatic?

I'm not sure about how that "other" fits in here and I can't quite get things to "count" correctly...I can get it to count...just not on a hit/collision.

I'm still working on it...looking for more examples to work with.

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 Topthink · Dec 13, 2016 at 07:59 PM 0
Share

Almost have it.

Okay, I have it so it will register hits but I figured out I need to use "IsTrigger" on one device. This results in the objects moving through each other. I still want them to collide and bounce and so forth. I'm trying to figure the settings to use to do this but as yet, can't figure it.

avatar image Topthink · Dec 13, 2016 at 09:32 PM 0
Share

Okay, I finally got everything fixed so it works. I had to use OnCollisionEvent. I tried for sometime to use OnTrigger but it wasn't quite what I needed.

Wow.

Took a long time to figure out how to do that. I should be through with my project (just for fun though) in about 2116, give or take. :)

avatar image
0

Answer by sisse008 · Dec 12, 2016 at 07:55 AM

add a collider to one of the objects, make sure both have rigid bodies:

void OnTriggerEnter(Collider other){ count++; }

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 Topthink · Dec 12, 2016 at 06:40 PM 0
Share

I'm going to look at this right away. Thanks.

avatar image Topthink · Dec 12, 2016 at 06:57 PM 0
Share

I've been reading up on this (I didn't even know this existed) and it seems exactly like what I want.

I going to play around with it a bit to try to get it in place with the proper syntax and in the proper location in the script.

Thanks.

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

102 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 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 avatar image avatar image avatar image avatar image

Related Questions

Rigidbody going through Colliders help! 1 Answer

collision is making its own vector 1 Answer

Collision not detecting 1 Answer

IsTouching() not working right after Colliders2D movement/instanciation 0 Answers

How can I make an object moving on its own bounce off walls 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