Created
September 24, 2019 14:39
-
-
Save MattRix/7bf12b4f913c85b565143c6a7d95e1dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class BackwardEulerController : MonoBehaviour | |
{ | |
public float gain = 200; //200-700; | |
public float damp = 20; //20-100; | |
public Transform target = null; | |
Vector3 velocity = Vector3.zero; | |
void Update() | |
{ | |
transform.position = Integrate(target.position,transform.position, ref velocity,gain,damp, Time.deltaTime); | |
} | |
static public Vector3 Integrate(Vector3 target, Vector3 current, ref Vector3 velocity, float gain, float damp, float dt) | |
{ | |
float g = damp * dt + gain * dt * dt; | |
g = 1.0f / (1.0f + g); | |
float _gain = gain * g; | |
float _damp = (damp + gain * dt) * g; | |
Vector3 diff = target - current; | |
Vector3 force = diff * _gain - velocity * _damp; | |
velocity += velocity + force * dt; | |
return current + velocity * dt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment