Detect multiple objects with spherecast

I am trying to make a simple Third-Person Combat.
The player model swings his sword, if a button is pressed.
I use a spherecast to identify which enemy is hit and it works.
Of course, if there are two enemys in front of the player, only one gets hit.

Is it possible to get the hit component for multiple GameObjects with spherecast?

I thought of get the hits by checking if the sword collides during animation,
but it is an android project, so I wonder if this would consume much power?

I also thought of using a sphere collider and let the script check which GameObjects
are inside and send them messages.

Has anyone experiences with this and could help me?
Maybe there is a good tutorial?
Thanks in advance.

1 Answer

1

How about Physics.SphereCastAll()?

Yes, thats totally it. I wasnt aware of this. Could someone explain to me how I use the hit array in c#? RaycastHit hit; if (Physics.SphereCast(char_pos, 0.8f, fwd, out hit, 1)){ hit.transform.gameObject.SendMessage("GotHit",damage); } How can I send the message to all Objects inside the sphere? Is it "foreach (RaycastHit q in hit)" ?

With this: RaycastHit[] hit= Physics.SphereCastAll(char_pos, 0.8f, fwd, 2.1f); foreach(RaycastHit q in hit){ transform.gameObject.SendMessage("GotHit", damage); } I dont get an error, but Unity says, that there is no reciever for the "send message". Whats wrong?

Do you have a method GotHit in any script attached to your game object? Please check example in [SendMessage doc][1]. [1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html

Yes, and it works for sure, because RaycastHit hit; if (Physics.SphereCast(char_pos, 0.8f, fwd, out hit, 1)){ hit.transform.gameObject.SendMessage("GotHit",damage); } works just fine.

You could use DontRequireReceiver flag as detailed in the docs. Or add a receiver to the object which receives the message.