SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. namespace Do.Spacegame
  4. {
  5.     public class Foliage : MonoBehaviour
  6.     {
  7.         [SerializeField] MeshFilter meshFilter = null;
  8.         [SerializeField] MeshRenderer meshRenderer = null;
  9.         [SerializeField] LayerMask layerMask = new LayerMask();
  10.         Spring spring = new Spring();
  11.  
  12.         static readonly string LAYER = "Level";
  13.         static readonly int LAYER_ORDER = 50;
  14.         [SerializeField] float BEND_FACTOR = 0.5f;
  15.         [SerializeField] float BEND_FORCE_ON_EXIT = 0.3f;
  16.         [SerializeField] float colliderHalfWidth = 0.5f;
  17.  
  18.         float enterOffset = 0;
  19.         float exitOffset = 0;
  20.         bool isBending = false;
  21.         bool isRebounding = false;
  22.  
  23.         void Awake()
  24.         {
  25.             meshRenderer.sortingLayerName = LAYER;
  26.             meshRenderer.sortingOrder = LAYER_ORDER;
  27.         }
  28.  
  29.         void LateUpdate()
  30.         {
  31.             if (!isRebounding)
  32.                 return;
  33.  
  34.             // apply the spring until its acceleration dies down
  35.             if (Mathf.Abs(spring.acceleration) < Mathf.Epsilon)
  36.             {
  37.                 setVertHorizontalOffset(0f);
  38.                 isRebounding = false;
  39.             }
  40.             else
  41.                 setVertHorizontalOffset(spring.simulate());
  42.         }
  43.  
  44.         void OnTriggerEnter2D(Collider2D collider)
  45.         {
  46.             if ((layerMask.value & 1 << collider.gameObject.layer) == 0)
  47.                 return;
  48.  
  49.             enterOffset = collider.transform.position.x - transform.position.x;
  50.         }
  51.  
  52.         void OnTriggerExit2D(Collider2D collider)
  53.         {
  54.             if ((layerMask.value & 1 << collider.gameObject.layer) == 0)
  55.                 return;
  56.  
  57.             // Apply a force in the opposite direction that we are currently bending
  58.             if (isBending)
  59.                 spring.applyForceStartingAtPosition(BEND_FORCE_ON_EXIT * Mathf.Sign(exitOffset), exitOffset);
  60.  
  61.             isBending = false;
  62.             isRebounding = true;
  63.         }
  64.  
  65.         void OnTriggerStay2D(Collider2D collider)
  66.         {
  67.             if ((layerMask.value & 1 << collider.gameObject.layer) == 0)
  68.                 return;
  69.  
  70.             float offset = collider.transform.position.x - transform.position.x;
  71.  
  72.             if (isBending || Mathf.Sign(enterOffset) != Mathf.Sign(offset))
  73.             {
  74.                 float radius = colliderHalfWidth + collider.bounds.size.x * 0.5f;
  75.                 exitOffset = map(offset, -radius, radius, -1f, 1f);
  76.                 setVertHorizontalOffset(exitOffset);
  77.  
  78.                 isRebounding = false;
  79.                 isBending = true;
  80.             }
  81.         }
  82.         void setVertHorizontalOffset(float offset)
  83.         {
  84.             Vector3[] vertices = meshFilter.mesh.vertices;
  85.  
  86.             vertices[2].x = -0.5f + (offset * BEND_FACTOR / transform.localScale.x);
  87.             vertices[3].x = +0.5f + (offset * BEND_FACTOR / transform.localScale.x);
  88.  
  89.             meshFilter.mesh.vertices = vertices;
  90.         }
  91.  
  92.         static float map(float value, float leftMin, float leftMax, float rightMin, float rightMax) => rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin);
  93.     }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top