- Home /
Creating ship energy shields effect
I am wanting to create an energy shield for ships in my game. I know I can create a simple collider / mesh for the shield and set it to invisible (or disable renderer) and then re-enable the renderer when it gets hit and then instantiate a ship shield collision effect prefab for the shield, but i am curious how to avoid instantiating a prefab of a shield effect.
I heard in another forum (this one): Forum post that i can do this.
If you are doing a spherical shield, you can probably put a sphere around your ship, then via script change the color and alpha where there is a collision. I believe the Space Dock widget from the widget contest did this.
How would i achieve this? If a shader is needed, how can I make one with strumpy shader editor?
The qualities I plan to achieve are the following:
An almost transparent sheild
When hit, the shield at the collision location glows and becomes less transparent and then fades back
An effect that can be added to the collision area (maybe using a prefab or emitter)
Mobile kept in mind, so efficiency is a concern
Not permanent / static (on a moving object and shield has health)
An example images of what I'm looking for:
Answer by Loius · Mar 08, 2013 at 02:54 AM
To get only the collision area to glow, you'll need an effect which occurs at the collision point (well, duh, but i'm getting to something). So if you get hit from front and back at the same time, you probably want to have the effect occur in two places, animating independently. All of that sounds like you'll really want to have multiple copies of the effect waiting to happen (instantiate them 'off' at the start, then activate them as necessary). It might be possible to use a single shader on a single sphere and grant it some spherical, multi-point glow, but that sounds extremely complex to me.
Probably the very simplest way is to create a simple ring shape in a modeling program, animate it as desired (could be as easy as scaling and moving a single bone), and run that animation whenever your want the shield to flash. Set the animated object's size to match the shield size and rotate it to face the impact point before playing the animation.
A more complex way, which can be made to make a far more 'beautiful' effect, is to use an alpha cutoff shader on a stepped-transparency image, like this one:
Put it on a curved surface, rotate the surface to face the impact point, and then set the material's cutoff value from zero to one to zero over time. Using Unity's default cutout shader, the code is renderer.material.SetFloat( "_Cutoff", value );
The drawback with the image method is that you'd need to add in a second cutoff to create a range of transparencies to display, if you want a 'ring' effect. It's also more complicated to create transparencies in a meaningful pattern like the multiple rings in your image.
And one last thing, if you use one of these methods, and have rings, and have two hits near each other, the effects can overlap and create interlocking rings. You could get around that by messing with the meshes and UV coordinates, or with a complex custom shader. I don't have the maths skills to be much help with that, though.
Answer by BenKurdziel · Mar 08, 2013 at 02:14 AM
Any ARGB or RGBA files have alphas. Of course, jpg's don't support transparency, so you'd need to use a png formatted material if you are importing a material. Apply the material to the sphere like you normally would, and adjust the alpha however you please for the initial value (invisible if you like).
Then, I believe the code you need will be something along the lines of:
renderer.material.color.a = 'whatever you want the alpha value to be here'
What this is doing is accessing the rendering component of your object, finding the material on it, and adjusting the alpha of the color attribute. I don't remember the range, though I suspect it's probably 0-255 like any of the other RGB Values.
Preferably, place this script on the sphere and make sure it has a Sphere Collider set to "is trigger", and check for:
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == 'tag of item that hits ship')
{
this.renderer.material.color.alpha = 0;
}
}
you can do another if statement of course if you want different code for different objects that hit it, or if you want the same code for multiple objects you can use "||" which means OR, which is done by holding Shift and Backward Slash at the same time
How about the only the collision area glows
, I think that will require a more advance shader to achieve this effects, and I even doubt that whether this effect can be achieved or not.
Your answer
Follow this Question
Related Questions
Fired projectile collision with player issue 1 Answer
Neon light or LED light 1 Answer
I want only the edges visible on my cube? 2 Answers
How to add atmospheric glow to a planet 2 Answers
Glow effect on transparent object 2 Answers