An open API service indexing awesome lists of open source software.

https://github.com/uzertekton/udonsharp-springdamp

Udon-optimized implementation of a damped harmonic oscillator for smoothing float values, angles, and Vector3s in UdonSharp.
https://github.com/uzertekton/udonsharp-springdamp

udon udonsharp unity vrchat

Last synced: 2 months ago
JSON representation

Udon-optimized implementation of a damped harmonic oscillator for smoothing float values, angles, and Vector3s in UdonSharp.

Awesome Lists containing this project

README

          

# UdonSharp SpringDamp

Udon-optimized implementation of a **damped harmonic oscillator** for smoothing float values, angles, and Vector3s in UdonSharp.

This is a good replacement for `Mathf.SmoothDamp`, `Mathf.SmoothDampAngle`, and `Vector3.SmoothDamp` with more precise control over damping and oscillations.

Especially with underdamping (zeta < 1), it can create very realistic and natural movements that feel springy and lively.

## Getting Started

1. Copy the `SpringDampUtils.cs` file into your Unity project’s `Assets` folder (or any subfolder).
2. In your UdonSharp scripts, include this utility by calling its static methods directly (see examples below).
3. (Optional) Use the simulator in `simulator/springdamp_simulator.py` for quick testing and prototyping! You can run it on somewhere like Jupyter Notebook.

## Usage Examples

### Smooth float values

```
float currentValue = 0f;
float targetValue = 10f;
float velocity = 0f;

void Update()
{
float deltaTime = Time.smoothDeltaTime; // Or Time.deltaTime, personal preference

// Use default omega0 (50f) and zeta (0.5f)
currentValue = SpringDampUtils.SpringDamp(currentValue, targetValue, ref velocity, deltaTime);

// Or customize omega0 (frequency) and zeta (damping ratio)
// float omega0 = 31.416f; // ~5 Hz natural frequency
// float zeta = 0.25f; // moderately damped, some oscillations
// currentValue = SpringDampUtils.SpringDamp(currentValue, targetValue, ref velocity, deltaTime, omega0, zeta);
}
```

### Smooth angles in degrees

```
float currentAngle = 0f;
float targetAngle = 90f;
float angularVelocity = 0f;

void Update()
{
float deltaTime = Time.smoothDeltaTime; // Or Time.deltaTime, personal preference

// Default parameters
currentAngle = SpringDampUtils.SpringDampAngle(currentAngle, targetAngle, ref angularVelocity, deltaTime);

// Custom parameters example
// float omega0 = 62.832f; // ~10 Hz
// float zeta = 0.5f; // heavily damped (default)
// currentAngle = SpringDampUtils.SpringDampAngle(currentAngle, targetAngle, ref angularVelocity, deltaTime, omega0, zeta);
}
```

### Smooth Vector3 values

```
Vector3 currentPos = Vector3.zero;
Vector3 targetPos = new Vector3(10f, 5f, 3f);
Vector3 velocity = Vector3.zero;

void Update()
{
float deltaTime = Time.smoothDeltaTime; // Or Time.deltaTime, personal preference

// Default parameters
currentPos = SpringDampUtils.SpringDampVector3(currentPos, targetPos, ref velocity, deltaTime);

// Custom parameters example
// float omega0 = 188.496f; // ~30 Hz, very fast response
// float zeta = 1.0f; // critically damped
// currentPos = SpringDampUtils.SpringDampVector3(currentPos, targetPos, ref velocity, deltaTime, omega0, zeta);
}
```

## Parameters

- `current` (float or Vector3 component): The current value of the property you want to smooth.
- `target` (float or Vector3 component): The target value you want to approach smoothly.
- `currentVelocity` (ref float or Vector3 component): The velocity (rate of change) value that is updated each frame to maintain smooth motion. **You need to declare a variable for this and persist it across calls.**

- `deltaTime` (float): The time step to advance the simulation, usually `Time.smoothDeltaTime` or `Time.deltaTime`.
- `omega0` (float, default 50f): Natural frequency in radians per second (rad/s). Controls how fast the value moves toward the target.
- Relationship to frequency (Hz):
`frequency (Hz) = omega0 / (2 * π) ≈ omega0 / 6.283`
- Common values:
- 1 Hz ≈ 6.283
- 5 Hz ≈ 31.416
- 8 Hz ≈ 50 (default)
- 10 Hz ≈ 62.832
- 30 Hz ≈ 188.496
- `zeta` (float, default 0.5f): Damping ratio (dimensionless). Controls oscillations and smoothness:
- `< 0`: Unstable (negative damping, diverges)
- `0`: Undamped (pure oscillation, no decay)
- `0 < zeta < 1`: Underdamped (oscillations with decay), e.g.
- 0.1: Light damping, lots of oscillation
- 0.25: Moderate damping, fewer oscillations
- 0.5: Heavy damping, smooth approach (default)
- `1`: Critically damped (fastest return without overshoot)
- `> 1`: Overdamped (no oscillations, slower return)

Feel free to customize `omega0` and `zeta` to tune responsiveness and oscillation behavior to your needs.

### Contact

Leave a message on my Discord server or DM me: https://discord.gg/yG4HnBM8Du