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
-1
Question by Dalek.skaro · Aug 30, 2015 at 06:20 PM · javascriptshadereffectsfixshield

How can i fix/properly use this code ?

hello, i have been given two codes to implement a shield effect, this is that effect that is commonly seen in sci-fi series and movies, in which the shield (normally a bubble of plasma) only "reveals" itself when it is hit by enemy fire, or other phenomena, like a solar flare. these codes are one script and one shader that i will post bellow

the script ( in javascript ) :

 #pragma strict
 
 private var EffectTime : float;
 
 private var RemainingTime : float[];
 
 private var ShieldColor : UnityEngine.Color;
 private var tempColor : UnityEngine.Color;
 private var hitCount : byte = 0;
 private var shieldHP : float;
 
 //alpha value of the ENTIRE shield in the moment of hit. minimum value is the shieldcolor alpha
 var tempAlpha : float = 0.01;
 //the time in ms while the above flashing happens
 var flashTime : float;
 //UI Text type object to show shield HP readout
 var textHP : UI.Text;
 
 function Start()
 {
     hitCount = 0;
     RemainingTime = new float[10];
     
     ShieldColor = GetComponent.<Renderer>().material.GetColor("_ShieldColor");
     EffectTime = GetComponent.<Renderer>().material.GetFloat("_EffectTime");
     shieldHP = GetComponent.<Renderer>().material.GetFloat("_ShieldHP");
     
     tempColor = ShieldColor;
     tempColor.a = Mathf.Clamp(tempAlpha, ShieldColor.a, 1);
 }
 
 function Update()
 {
     //shield regeneration
     shieldHP = Mathf.Clamp(shieldHP + 0.005 * Time.deltaTime, 0, 1);
     
     //if there is shield turn on collider
     if(shieldHP > 0.001)
     {
         GetComponent.<Renderer>().material.SetFloat("_ShieldHP", shieldHP);
         this.GetComponent.<Collider>().enabled = true;
     }
     
     if(Mathf.Max(RemainingTime) > 0)
     {
         if(Mathf.Max(RemainingTime) < (EffectTime - flashTime))
         {
             GetComponent.<Renderer>().material.SetColor("_ShieldColor", ShieldColor);
         }
         
         for(var i=0; i<10; i++)
         {
             RemainingTime[i] = Mathf.Clamp(RemainingTime[i] - Time.deltaTime * 1000, 0, EffectTime);                
             GetComponent.<Renderer>().material.SetFloat("_RemainingTime" + i.ToString(), RemainingTime[i]);
             GetComponent.<Renderer>().material.SetVector("_Position" + i.ToString(), transform.FindChild("hitpoint" + i.ToString()).position);        
         }
     }
     
     textHP.text = "Shield:" + (Mathf.RoundToInt(shieldHP * 1000)).ToString();
 }
     
 function OnCollisionEnter(collision : Collision)
 {
     //draining shield HP
     shieldHP = Mathf.Clamp(shieldHP - 0.001, 0, 1);
     
     //if there is no shield, turn off collider, so the shots can hit the ship instead
     if(shieldHP <= 0.001)
     {
         shieldHP = 0;
         this.GetComponent.<Collider>().enabled = false;
         GetComponent.<Renderer>().material.SetFloat("_ShieldHP", shieldHP);
     }
     else
     {
         for (var contact : ContactPoint in collision.contacts)
         {        
             GetComponent.<Renderer>().material.SetColor("_ShieldColor", tempColor);
             GetComponent.<Renderer>().material.SetFloat("_ShieldHP", shieldHP);
             transform.FindChild("hitpoint" + hitCount.ToString()).position = contact.point;
             RemainingTime[hitCount] = EffectTime;
             hitCount++;
             if(hitCount > 9)
                 hitCount = 0;
         }
     }
 }

and the shader :

 Shader "Shieldeffect" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "White" {}
           //_BumpMap ("Bumpmap", 2D) = "bump" {}
         _MaxDistance ("Effect Size", float) = 10
         _ShieldColor ("Color (RGBA)", Color) = (1, 1, 1, 0)
         _EmissionColor ("Emission color (RGB)", Color) = (1, 1, 1)
         _EffectTime ("Effect Time (ms)", float) = 500
         
         [HideInInspector]_ShieldHP ("Shield HP", float) = 1
         
         [HideInInspector]_RemainingTime0 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime1 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime2 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime3 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime4 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime5 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime6 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime7 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime8 ("Remaining Time (ms)", float) = 0
         [HideInInspector]_RemainingTime9 ("Remaining Time (ms)", float) = 0
         
         [HideInInspector]_Position0 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position1 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position2 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position3 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position4 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position5 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position6 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position7 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position8 ("Collision", Vector) = (-1, -1, -1, -1)
         [HideInInspector]_Position9 ("Collision", Vector) = (-1, -1, -1, -1)
     }
     
     SubShader {
         Tags {"QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent"}
         LOD 2000
         Cull OFF
       
         CGPROGRAM
           #pragma surface surf NoLight alpha:fade
           #pragma target 3.0
           
           //turn off lighting
           half4 LightingNoLight (SurfaceOutput s, half3 lightDir, half atten)
           {
               fixed4 c;
             c.rgb = s.Albedo; 
             c.a = s.Alpha;
             return c;
         }
           
          struct Input {
              float2 uv_MainTex;
             //float2 uv_BumpMap;
              float3 worldPos;
           };
       
           float4 _Position0;
           float4 _Position1;
           float4 _Position2;
           float4 _Position3;
           float4 _Position4;
           float4 _Position5;
           float4 _Position6;
           float4 _Position7;
           float4 _Position8;
           float4 _Position9;
           
           float  _MaxDistance;
           float4 _ShieldColor;
           float3 _EmissionColor;
           float  _EffectTime;
           float  _ShieldHP;
           
           float tempA;
           
           float  _RemainingTime0;
           float  _RemainingTime1;
           float  _RemainingTime2;
           float  _RemainingTime3;
           float  _RemainingTime4;
           float  _RemainingTime5;
           float  _RemainingTime6;
           float  _RemainingTime7;
           float  _RemainingTime8;
           float  _RemainingTime9;
           
           sampler2D _MainTex;
           //sampler2D _BumpMap;
       
           void surf (Input IN, inout SurfaceOutput o)
           {
             half4 c = tex2D (_MainTex, IN.uv_MainTex);
             
             o.Albedo = c.rgb * _ShieldColor.rgb;
             //o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
             o.Emission = c.rgb * _EmissionColor.rgb;
             o.Alpha = _ShieldColor.a * _ShieldHP;
             
             float myDist0 = distance(_Position0.xyz, IN.worldPos);
             float myDist1 = distance(_Position1.xyz, IN.worldPos);
             float myDist2 = distance(_Position2.xyz, IN.worldPos);
             float myDist3 = distance(_Position3.xyz, IN.worldPos);
             float myDist4 = distance(_Position4.xyz, IN.worldPos);
             float myDist5 = distance(_Position5.xyz, IN.worldPos);
             float myDist6 = distance(_Position6.xyz, IN.worldPos);
             float myDist7 = distance(_Position7.xyz, IN.worldPos);
             float myDist8 = distance(_Position8.xyz, IN.worldPos);
             float myDist9 = distance(_Position9.xyz, IN.worldPos);
                 
             if(_RemainingTime0 > 0 && myDist0 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime0 / _EffectTime - myDist0 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime1 > 0 && myDist1 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime1 / _EffectTime - myDist1 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime2 > 0 && myDist2 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime2 / _EffectTime - myDist2 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime3 > 0 && myDist3 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime3 / _EffectTime - myDist3 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime4 > 0 && myDist4 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime4 / _EffectTime - myDist4 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime5 > 0 && myDist5 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime5 / _EffectTime - myDist5 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime6 > 0 && myDist6 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime6 / _EffectTime - myDist6 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime7 > 0 && myDist7 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime7 / _EffectTime - myDist7 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime8 > 0 && myDist8 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime8 / _EffectTime - myDist8 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
             if(_RemainingTime9 > 0 && myDist9 < _MaxDistance)
             {
                 tempA = clamp(_RemainingTime9 / _EffectTime - myDist9 / _MaxDistance, _ShieldColor.a, 1) * _ShieldHP;
                 if(tempA > o.Alpha)
                     o.Alpha = tempA;
             }
         }   
           
           ENDCG
     } 
     Fallback "Transparent/Diffuse"
 }

but i can't make them work properly, first thing is that the shader only acepts a texture and i dont know how to convert a material, that i am satisfied with, in that shader.

Since it has no Rendering Mode : transparent ; and the Main Maps of : Albedo and Metallic [ Smoothness ].

Lastly it appears to have a HP (health points) variable written in the middle of the code, how do i integrate this variable with an actual HP bar ?

i tried to test this shader and script in an object, but it fully reveals "reveals"(renders) the shield, rather than revealing only the part which was hit. How can i fix these problems ?

Or is it that i am not using these codes properly ?

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

30 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

Related Questions

Unity 5 Shader : Pulsing Emission range via scripting 2 Answers

URP Camera.RenderWithShader(..) doesn't seem to work? 0 Answers

Best way to achieve color by data? 0 Answers

Effects that I made in a HDRP project don't work in a URP project. 0 Answers

Triggering Image Effects in Unity 5 0 Answers


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