Advertisement
Guest User

Untitled

a guest
Jan 6th, 2022
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class Electric : MonoBehaviour
  7. {
  8.     [SerializeField, Range(0, 1f)] float om;
  9.     public float voltage;
  10.     [SerializeField] List<Electric> targets;
  11.  
  12.     [SerializeField] bool conts;
  13.     [SerializeField] float contsvolt;
  14.  
  15.     private void Update()
  16.     {
  17.         if (voltage > 0.1f)
  18.         {
  19.             voltage *= om;
  20.         }
  21.         else
  22.         {
  23.             voltage = 0;
  24.         }
  25.     }
  26.  
  27.     void LateUpdate()
  28.     {
  29.  
  30.  
  31.         if (targets != null)
  32.         {
  33.             foreach (Electric target in targets)
  34.             {
  35.                 if (voltage > target.voltage)
  36.                 {
  37.                     float tempvolt;
  38.                     tempvolt = voltage;
  39.                     voltage = Mathf.Lerp(voltage, target.voltage, Time.deltaTime*50f);
  40.                     target.voltage = Mathf.Lerp(target.voltage, tempvolt, Time.deltaTime * 50f);
  41.                 }
  42.             }
  43.         }
  44.         if (conts)
  45.         {
  46.             voltage = contsvolt;
  47.         }
  48.     }
  49.  
  50.     void OnDrawGizmos()
  51.     {
  52.         UnityEditor.Handles.Label(transform.position, voltage.ToString());
  53.     }
  54.  
  55.     private void OnCollisionEnter(Collision collision)
  56.     {
  57.         Electric item = collision.gameObject.GetComponent<Electric>();
  58.         if (item!=null)
  59.         {
  60.            
  61.             targets.Add(item);
  62.         }
  63.  
  64.  
  65.  
  66.     }
  67.     private void OnCollisionExit(Collision collision)
  68.     {
  69.         Electric item = collision.gameObject.GetComponent<Electric>();
  70.         if (item != null)
  71.         {
  72.  
  73.             targets.Remove(item);
  74.         }
  75.     }
  76.  
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement