Advertisement
Guest User

Hiteffect.cs

a guest
Nov 13th, 2019
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class HitEffect : MonoBehaviour
  6. {
  7.  
  8.     MaterialPropertyBlock props;
  9.  
  10.     Renderer[] renderers;
  11.     //Animator anim; uncomment if you have an animator attached and a hit animation
  12.     // Start is called before the first frame update
  13.     void Start()
  14.     {
  15.         props = new MaterialPropertyBlock();
  16.         renderers = transform.GetComponentsInChildren<Renderer>();
  17.         // anim = GetComponent<Animator>(); uncomment if you have an animator attached and a hit animation
  18.  
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         if (Input.GetKeyDown(KeyCode.H))
  25.         {
  26.             // anim.Play("Hit"); uncomment if you have an animator attached and a hit animation
  27.             StartCoroutine(HitShaderEffect());
  28.         }
  29.     }
  30.  
  31.     IEnumerator HitShaderEffect()
  32.     {
  33.         props.SetFloat("_Hit", 1f);
  34.         for (int i = 0; i < renderers.Length; i++)
  35.         {
  36.             renderers[i].SetPropertyBlock(props);
  37.         }
  38.         yield return new WaitForSeconds(0.1f);
  39.         props.SetFloat("_Hit", 0f);
  40.         for (int i = 0; i < renderers.Length; i++)
  41.         {
  42.             renderers[i].SetPropertyBlock(props);
  43.         }
  44.  
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement