r2mods/ilspy_dump/ror2_csproj/FloatPID.cs

61 lines
1.1 KiB
C#
Raw Normal View History

2024-10-04 07:26:37 +00:00
using UnityEngine;
public class FloatPID : MonoBehaviour
{
[Tooltip("PID Constants.")]
public Vector3 PID = new Vector3(1f, 0f, 0f);
public float gain = 1f;
[Tooltip("The value we are currently at.")]
[HideInInspector]
public float inputFloat;
[HideInInspector]
[Tooltip("The value we want to be at.")]
public float targetFloat;
[Tooltip("Value output from PID controller; what we read.")]
[HideInInspector]
public float outputFloat;
public float timeBetweenUpdates;
private float timer;
private float errorSum;
private float deltaError;
private float lastError;
private float lastTimer;
public bool automaticallyUpdate;
private void Start()
{
}
private void FixedUpdate()
{
timer += Time.fixedDeltaTime;
if (automaticallyUpdate && timer > timeBetweenUpdates)
{
timer -= timeBetweenUpdates;
outputFloat = UpdatePID();
}
}
public float UpdatePID()
{
float num = timer - lastTimer;
lastTimer = timer;
float num2 = targetFloat - inputFloat;
errorSum += num2 * num;
deltaError = (num2 - lastError) / num;
lastError = num2;
return (num2 * PID.x + errorSum * PID.y + deltaError * PID.z) * gain;
}
}