WOW, tried PID and I always got weird results, so I wrote my code :
to put in a file called PhysicsHelper.cs
using UnityEngine;
using System.Collections;
public static class PhysicsHelper
{
public static void TorqueLookAtPoint(Rigidbody rigidbody, Vector3 point, float force, float damper = 0f)
{
Vector3 direction = point - rigidbody.position;
TorqueLookToward(rigidbody, direction, force, damper);
}
public static void TorqueLookToward(Rigidbody rigidbody, Vector3 direction, float force, float damper = 0f)
{
Vector3 p = rigidbody.position;
Vector3 forward = rigidbody.transform.forward; // axis we are rotating
Vector3 cross = Vector3.Cross(forward, direction);
float angleDiff = Vector3.Angle(forward, direction);
angleDiff = Mathf.Sqrt(angleDiff);
rigidbody.AddTorque(cross * angleDiff * force, ForceMode.Acceleration);
rigidbody.AddTorque(-rigidbody.angularVelocity * damper, ForceMode.Acceleration);
//rigidbody.AddTorque(cross * angleDiff * force);
//Debug.Log(direction);
//Debug.DrawLine(p, p + direction.normalized, Color.yellow, .05f);
//Debug.DrawLine(p, p + rigidbody.angularVelocity, Color.yellow);
//Debug.DrawLine(p, p + new Vector3(rigidbody.angularVelocity.x, 0, 0), Color.red);
//Debug.DrawLine(p, p + new Vector3(0,rigidbody.angularVelocity.y, 0), Color.green);
//Debug.DrawLine(p, p + new Vector3(0,0,rigidbody.angularVelocity.z), Color.blue);
}
}
Example of use :
Rigidbody rigid = transform.GetComponent<Rigidbody>();
Vector3 direction = Vector3.one;
float force = 100f / Time.fixedDeltaTime;
float damper = 50f;
PhysicsHelper.TorqueLookToward(rigid, direction, force, damper);
Finally just adjust the force and set the dampening to not have your object’s direction pass the direction and have a spring effect.
It may fit for most of your cases, with a minimum problems
Beware, if you’d like to change the deltatime per example, the force changes and needed dampening
too (but it’s just proportional to speed). Anyway, best solution I’ve found
>>> GIF <<<
You, @Duck and @Eric5h5 ... geniuses :D
– KleptomaniacAlright, well to be honest, I was hoping for something that would also work outside of unity... I have a 2d game engine where you would add rotational forces the same way as in unity, and was hoping for a solution that would be applicable for projects in there as well as in my 3d game.
– anon42704792Wow, this is brilliant. Thanks!
– iceydeeI would be very interested in seeing this pseudo code converted into tested/working code. I have tried implementing it here https://github.com/Will9371/Playcraft/tree/master/Assets/Playcraft/Quality%20of%20Life/Physics/PID by adapting the PID controller from your other post (I've also converted it to C#, extended to 3 axis rotation, combined it with PID movement, and have it following a target transform), but it moves erratically. The other post's code works OK, but doesn't provide physics-based control since it ultimately applies rotation directly.
– wpetilloYou have successfully deployed at https://github.com/Will9371/Playcraft/tree/master/Assets/Playcraft/Quality%20of%20Life/Physics/PID https://sprunki-retake.lol
– SprunkiRetake