Guest User

Untitled

a guest
Jan 16th, 2021
10,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Spring {
  4.     private float strength;
  5.     private float damper;
  6.     private float target;
  7.     private float velocity;
  8.     private float value;
  9.  
  10.     public void Update(float deltaTime) {
  11.         var direction = target - value >= 0 ? 1f : -1f;
  12.         var force = Mathf.Abs(target - value) * strength;
  13.         velocity += (force * direction - velocity * damper) * deltaTime;
  14.         value += velocity * deltaTime;
  15.     }
  16.  
  17.     public void Reset() {
  18.         velocity = 0f;
  19.         value = 0f;
  20.     }
  21.        
  22.     public void SetValue(float value) {
  23.         this.value = value;
  24.     }
  25.        
  26.     public void SetTarget(float target) {
  27.         this.target = target;
  28.     }
  29.  
  30.     public void SetDamper(float damper) {
  31.         this.damper = damper;
  32.     }
  33.        
  34.     public void SetStrength(float strength) {
  35.         this.strength = strength;
  36.     }
  37.  
  38.     public void SetVelocity(float velocity) {
  39.         this.velocity = velocity;
  40.     }
  41.        
  42.     public float Value => value;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment