Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: tim.holman on May 07, 2012, 02:36:29 PM

Title: Sphere casting
Post by: tim.holman on May 07, 2012, 02:36:29 PM
Before I posted this under the request actions page, I wanted to make sure there's nothing in place to handle this already.

I would like to use a sphere cast for a shot gun mechanic. The sphere would slowly spread out after being shot to simulate a shot gun blast (with shrapnel being spread in an even radius around the center.) I was using game objects that had colliders, projecting them out and reading the collision, but we've decided that sphere casting would be more cost-effective way of achieving the same effect.

Is there currently a way to do something like this? Any thoughts/ideas/suggestions would be much appreciated. Thank you!
Title: Re: Sphere casting
Post by: justifun on May 07, 2012, 03:14:07 PM
Not 100% sure if this is want you want to do, but why not just use a particle system with its emitter set to 'semi sphere"?

Also from a game design stand point.  Typically bullets arn't actually ever used for a fast gun type shooting like a shotgun.  Normally there's an effect of muzzle flash, and the bullets are assumed to be shot.

What you would do instead is calculate if the player is aiming towards the enemy and within the required range and simply deal damage without using collision detection at all.  This is far less expensive computational wise and you dont' have to deal with collisions mis firing etc.

eg: if the player is aiming towards the enemy and within 10 feet, deal 50 damage each time the player presses the fire button.  no bullets required.
Title: Re: Sphere casting
Post by: Alex Chouls on May 07, 2012, 04:39:18 PM
It might also be interesting to use a frustum check:
http://unity3d.com/support/documentation/ScriptReference/GeometryUtility.TestPlanesAABB.html

You could put a dummy camera on the gun and tweak its view frustum to represent the spread of the shotgun, so you have a nice visual gizmo to work with. Then a custom action that wrapped CalculateFrustumPlanes and TestPlanesAABB could send Hit and NotHit events. Should be a pretty easy custom action to write...

Although this doesn't take blocking objects into account.

Maybe you use it as a cheap phase 1 check (is the target visible to the gun?), followed by a few Raycasts for the actual shotgun pellets.

It all depends on how accurate you need to be...
Title: Re: Sphere casting
Post by: tim.holman on May 07, 2012, 06:48:25 PM
Thanks Alex and Justin!

Justin: I think I was a bit unclear. What you suggested was originally what I was asking for a solution for. What I meant was that we were formerly using sphere colliders to simulate bullets, and I want to use a sphere cast instead because as you mentioned, collision detection isn't quite cost effective in this scenario. Unity says to basically think of a sphere cast as a thick ray cast. I was hoping to use something like this:

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

Alex: I like the idea of visually being able to see the damage radius. I think I'll try to put something together more along those lines. But still, a "SphereCast" action might be a nice addition to Playmaker! Thank you again.
Title: Re: Sphere casting
Post by: Alex Chouls on May 07, 2012, 06:56:14 PM
Ahhh, I didn't realize Unity actually had a SphereCast! Learn something new every day! ;D
Title: Re: Sphere casting
Post by: FractalCore on May 09, 2012, 01:45:45 AM
Damn, Sphere Casting sounds awesome. Could we have one of those, please :)