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);
}
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.
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.
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. :)
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++; }
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.